Class: MiyazakiResistance::Base
- Inherits:
-
Object
- Object
- MiyazakiResistance::Base
- Defined in:
- lib/miyazaki_resistance/base.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
- .count(args = {}) ⇒ Object
- .create(args) ⇒ Object
- .find(first, args = {}) ⇒ Object
- .find_by_id(target) ⇒ Object
- .find_by_query(mode, args = {}) ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(args) ⇒ Object
- #destroy ⇒ Object
-
#initialize(args = nil) ⇒ Base
constructor
A new instance of Base.
- #new_record? ⇒ Boolean
- #save ⇒ Object
- #update_attributes(args) ⇒ Object
Constructor Details
#initialize(args = nil) ⇒ Base
Returns a new instance of Base.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/miyazaki_resistance/base.rb', line 5 def initialize(args = nil) args ||= {} self.id = nil args.each do |key, value| if key.is_a?(String) && key.empty? key = :id value = value.to_i else case self.class.all_columns[key.to_s] when :integer value = value.to_i when :string value = value.to_s when :datetime value = Time.at(value.to_i) unless value.is_a?(Time) when :date unless value.is_a?(Date) time = Time.at(value.to_i) value = Date.new(time.year, time.month, time.day) end end end self.__send__("#{key}=", value) end end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/miyazaki_resistance/base.rb', line 3 def id @id end |
Class Method Details
.count(args = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/miyazaki_resistance/base.rb', line 106 def count(args = {}) con = read_connection if args.empty? kaeru_timeout{con.rnum} else query = TokyoTyrant::RDBQRY.new(con) query = make_conditions(query, args[:conditions]) kaeru_timeout{query.search}.size end rescue TimeoutError remove_pool(con) retry end |
.create(args) ⇒ Object
120 121 122 123 124 |
# File 'lib/miyazaki_resistance/base.rb', line 120 def create(args) inst = self.new(args) inst.save inst end |
.find(first, args = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/miyazaki_resistance/base.rb', line 73 def find(first, args={}) case first.class.name when "Fixnum" find_by_id(first) when "Array" find_by_ids(first) when "Symbol" find_by_query(first, args) else raise ArgumentError end end |
.find_by_id(target) ⇒ Object
102 103 104 |
# File 'lib/miyazaki_resistance/base.rb', line 102 def find_by_id(target) find_by_ids([target]) end |
.find_by_query(mode, args = {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/miyazaki_resistance/base.rb', line 86 def find_by_query(mode, args={}) con = read_connection query = TokyoTyrant::RDBQRY.new(con) limit = (mode == :first ? 1 : args[:limit]) query = make_limit(query, limit, args[:offset]) query = make_order(query, args[:order]) query = make_conditions(query, args[:conditions]) results = kaeru_timeout{query.searchget}.map{|r| self.new(r)} limit.to_i == 1 ? results.first : results rescue TimeoutError remove_pool(con) retry end |
Instance Method Details
#attributes ⇒ Object
58 59 60 61 62 |
# File 'lib/miyazaki_resistance/base.rb', line 58 def attributes hash = {} self.class.all_columns.keys.each{|key| hash.update(key => self.__send__(key))} hash end |
#attributes=(args) ⇒ Object
64 65 66 |
# File 'lib/miyazaki_resistance/base.rb', line 64 def attributes=(args) args.each {|key, value| self.__send__("#{key}=", value)} end |
#destroy ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/miyazaki_resistance/base.rb', line 49 def destroy raise NewRecordError if new_record? con = write_connection kaeru_timeout{con.out(self.id)} rescue TimeoutError remove_pool(con) retry end |
#new_record? ⇒ Boolean
68 69 70 |
# File 'lib/miyazaki_resistance/base.rb', line 68 def new_record? self.id.nil? end |
#save ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/miyazaki_resistance/base.rb', line 32 def save time_column_check con = write_connection self.id = kaeru_timeout{con.genuid.to_i} if new_record? kaeru_timeout {con.put(self.id, raw_attributes)} rescue TimeoutError remove_pool(con) retry end |
#update_attributes(args) ⇒ Object
43 44 45 46 47 |
# File 'lib/miyazaki_resistance/base.rb', line 43 def update_attributes(args) raise NewRecordError if new_record? attributes = args save end |