Class: ActiveHash::Base
- Inherits:
-
Object
- Object
- ActiveHash::Base
- Defined in:
- lib/active_hash/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.field_names ⇒ Object
readonly
Returns the value of attribute field_names.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
- .all ⇒ Object
- .auto_assign_fields(array_of_hashes) ⇒ Object
-
.base_class ⇒ Object
Needed for ActiveRecord polymorphic associations.
- .configuration_for_custom_finder(finder_name) ⇒ Object
- .count ⇒ Object
- .create(attributes = {}) ⇒ Object
- .create!(attributes = {}) ⇒ Object
- .data=(array_of_hashes) ⇒ Object
- .define_custom_find_all_method(field_name) ⇒ Object
- .define_custom_find_method(field_name) ⇒ Object
- .define_getter_method(field, default_value) ⇒ Object
- .define_interrogator_method(field) ⇒ Object
- .delete_all ⇒ Object
- .field(field_name, options = {}) ⇒ Object
- .fields(*args) ⇒ Object
- .find(id, *args) ⇒ Object
- .find_by_id(id) ⇒ Object
- .insert(record) ⇒ Object
- .method_missing(method_name, *args) ⇒ Object
- .next_id ⇒ Object
- .respond_to?(method_name) ⇒ Boolean
- .transaction ⇒ Object
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
- #id ⇒ Object (also: #quoted_id)
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #new_record? ⇒ Boolean
- #readonly? ⇒ Boolean
- #save ⇒ Object (also: #save!)
- #to_param ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
201 202 203 204 |
# File 'lib/active_hash/base.rb', line 201 def initialize( = {}) .symbolize_keys! @attributes = end |
Class Attribute Details
.field_names ⇒ Object (readonly)
Returns the value of attribute field_names.
5 6 7 |
# File 'lib/active_hash/base.rb', line 5 def field_names @field_names end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
199 200 201 |
# File 'lib/active_hash/base.rb', line 199 def attributes @attributes end |
Class Method Details
.all ⇒ Object
45 46 47 |
# File 'lib/active_hash/base.rb', line 45 def all @records || [] end |
.auto_assign_fields(array_of_hashes) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/active_hash/base.rb', line 176 def auto_assign_fields(array_of_hashes) (array_of_hashes || []).inject([]) do |array, row| row.symbolize_keys! row.keys.each do |key| unless key.to_s == "id" array << key end end array end.uniq.each do |key| field key end end |
.base_class ⇒ Object
Needed for ActiveRecord polymorphic associations
193 194 195 |
# File 'lib/active_hash/base.rb', line 193 def base_class ActiveHash::Base end |
.configuration_for_custom_finder(finder_name) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/active_hash/base.rb', line 116 def configuration_for_custom_finder(finder_name) if finder_name.to_s.match(/^find_(all_)?by_(.*)/) { :all? => !!$1, :fields => $2.split('_and_') } end end |
.count ⇒ Object
49 50 51 |
# File 'lib/active_hash/base.rb', line 49 def count all.length end |
.create(attributes = {}) ⇒ Object
33 34 35 36 37 |
# File 'lib/active_hash/base.rb', line 33 def create(attributes = {}) record = new(attributes) record.save record end |
.create!(attributes = {}) ⇒ Object
39 40 41 42 43 |
# File 'lib/active_hash/base.rb', line 39 def create!(attributes = {}) record = new(attributes) record.save! record end |
.data=(array_of_hashes) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_hash/base.rb', line 7 def data=(array_of_hashes) @records = nil write_inheritable_attribute(:data, array_of_hashes) if array_of_hashes auto_assign_fields( array_of_hashes ) array_of_hashes.each do |hash| insert new(hash) end end end |
.define_custom_find_all_method(field_name) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/active_hash/base.rb', line 161 def define_custom_find_all_method(field_name) method_name = "find_all_by_#{field_name}" unless singleton_methods.include?(method_name) .instance_eval do unless singleton_methods.include?(method_name) define_method(method_name) do |name| all.select {|record| record.send(field_name) == name } end end end end end |
.define_custom_find_method(field_name) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/active_hash/base.rb', line 148 def define_custom_find_method(field_name) method_name = "find_by_#{field_name}" unless singleton_methods.include?(method_name) .instance_eval do define_method(method_name) do |name| all.detect {|record| record.send(field_name) == name } end end end end |
.define_getter_method(field, default_value) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/active_hash/base.rb', line 127 def define_getter_method(field, default_value) unless instance_methods.include?(field.to_s) define_method(field) do attributes[field] || default_value end end end |
.define_interrogator_method(field) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/active_hash/base.rb', line 137 def define_interrogator_method(field) method_name = "#{field}?" unless instance_methods.include?(method_name) define_method(method_name) do send(field).present? end end end |
.delete_all ⇒ Object
59 60 61 |
# File 'lib/active_hash/base.rb', line 59 def delete_all @records = [] end |
.field(field_name, options = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/active_hash/base.rb', line 87 def field(field_name, = {}) @field_names ||= [] @field_names << field_name define_getter_method(field_name, [:default]) define_interrogator_method(field_name) define_custom_find_method(field_name) define_custom_find_all_method(field_name) end |
.fields(*args) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/active_hash/base.rb', line 80 def fields(*args) = args. args.each do |field| field(field, ) end end |
.find(id, *args) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_hash/base.rb', line 63 def find(id, *args) case id when :all all when Array all.select {|record| id.map(&:to_i).include?(record.id) } else find_by_id(id) end end |
.find_by_id(id) ⇒ Object
74 75 76 |
# File 'lib/active_hash/base.rb', line 74 def find_by_id(id) all.detect {|record| record.id == id.to_i} end |
.insert(record) ⇒ Object
18 19 20 21 22 |
# File 'lib/active_hash/base.rb', line 18 def insert(record) @records ||= [] record.attributes[:id] ||= next_id @records << record end |
.method_missing(method_name, *args) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/active_hash/base.rb', line 107 def method_missing(method_name, *args) return super unless respond_to? method_name config = configuration_for_custom_finder(method_name) attribute_pairs = config[:fields].zip(args) matches = all.select { |base| attribute_pairs.all? { |field, value| base.send(field).to_s == value.to_s } } config[:all?] ? matches : matches.first end |
.next_id ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/active_hash/base.rb', line 24 def next_id max_record = all.max {|a, b| a.id <=> b.id } if max_record.nil? 1 elsif max_record.id.is_a?(Numeric) max_record.id.succ end end |
.respond_to?(method_name) ⇒ Boolean
97 98 99 100 101 102 103 104 105 |
# File 'lib/active_hash/base.rb', line 97 def respond_to?(method_name) super || begin config = configuration_for_custom_finder(method_name) config && config[:fields].all? do |field| field_names.include?(field.to_sym) || field.to_sym == :id end end end |
.transaction ⇒ Object
53 54 55 56 57 |
# File 'lib/active_hash/base.rb', line 53 def transaction yield rescue ActiveRecord::Rollback end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
224 225 226 |
# File 'lib/active_hash/base.rb', line 224 def eql?(other) other.instance_of?(self.class) and not id.nil? and (id == other.id) end |
#hash ⇒ Object
230 231 232 |
# File 'lib/active_hash/base.rb', line 230 def hash id.hash end |
#id ⇒ Object Also known as: quoted_id
206 207 208 |
# File 'lib/active_hash/base.rb', line 206 def id attributes[:id] ? attributes[:id] : nil end |
#new_record? ⇒ Boolean
212 213 214 |
# File 'lib/active_hash/base.rb', line 212 def new_record? ! self.class.all.include?(self) end |
#readonly? ⇒ Boolean
216 217 218 |
# File 'lib/active_hash/base.rb', line 216 def readonly? true end |
#save ⇒ Object Also known as: save!
234 235 236 237 |
# File 'lib/active_hash/base.rb', line 234 def save self.class.insert(self) true end |
#to_param ⇒ Object
220 221 222 |
# File 'lib/active_hash/base.rb', line 220 def to_param id.to_s end |
#valid? ⇒ Boolean
241 242 243 |
# File 'lib/active_hash/base.rb', line 241 def valid? true end |