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(options = {}) ⇒ 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 (also: add)
- .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
- .define_setter_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
- .mark_clean ⇒ Object
- .mark_dirty ⇒ Object
- .method_missing(method_name, *args) ⇒ Object
- .next_id ⇒ Object
- .reload ⇒ Object
- .respond_to?(method_name, include_private = false) ⇒ Boolean
- .the_meta_class ⇒ Object
- .transaction ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #cache_key ⇒ Object
- #destroyed? ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
- #id ⇒ Object (also: #quoted_id)
- #id=(id) ⇒ Object
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #marked_for_destruction? ⇒ Boolean
- #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.
273 274 275 276 277 278 279 |
# File 'lib/active_hash/base.rb', line 273 def initialize( = {}) .symbolize_keys! @attributes = .each do |key, value| send "#{key}=", value end end |
Class Attribute Details
.field_names ⇒ Object (readonly)
Returns the value of attribute field_names.
9 10 11 |
# File 'lib/active_hash/base.rb', line 9 def field_names @field_names end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
271 272 273 |
# File 'lib/active_hash/base.rb', line 271 def attributes @attributes end |
Class Method Details
.all(options = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/active_hash/base.rb', line 60 def all(={}) if .has_key?(:conditions) (@records || []).select do |record| [:conditions].all? {|col, match| record[col] == match} end else @records || [] end end |
.auto_assign_fields(array_of_hashes) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/active_hash/base.rb', line 229 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
246 247 248 |
# File 'lib/active_hash/base.rb', line 246 def base_class ActiveHash::Base end |
.configuration_for_custom_finder(finder_name) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/active_hash/base.rb', line 153 def configuration_for_custom_finder(finder_name) if finder_name.to_s.match(/^find_(all_)?by_(.*?)(!)?$/) && !($1 && $3) { :all? => !!$1, :bang? => !!$3, :fields => $2.split('_and_') } end end |
.count ⇒ Object
70 71 72 |
# File 'lib/active_hash/base.rb', line 70 def count all.length end |
.create(attributes = {}) ⇒ Object Also known as: add
45 46 47 48 49 50 |
# File 'lib/active_hash/base.rb', line 45 def create(attributes = {}) record = new(attributes) record.save mark_dirty record end |
.create!(attributes = {}) ⇒ Object
54 55 56 57 58 |
# File 'lib/active_hash/base.rb', line 54 def create!(attributes = {}) record = new(attributes) record.save! record end |
.data=(array_of_hashes) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/active_hash/base.rb', line 17 def data=(array_of_hashes) mark_dirty @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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/active_hash/base.rb', line 212 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 |*args| = args. identifier = args[0] all.select { |record| record.send(field_name) == identifier } end end end end end |
.define_custom_find_method(field_name) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/active_hash/base.rb', line 197 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 |*args| = args. identifier = args[0] all.detect { |record| record.send(field_name) == identifier } end end end end |
.define_getter_method(field, default_value) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/active_hash/base.rb', line 165 def define_getter_method(field, default_value) unless instance_methods.include?(field.to_s) define_method(field) do attributes[field].nil? ? default_value : attributes[field] end end end |
.define_interrogator_method(field) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/active_hash/base.rb', line 186 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 |
.define_setter_method(field) ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/active_hash/base.rb', line 175 def define_setter_method(field) method_name = "#{field}=" unless instance_methods.include?(method_name) define_method(method_name) do |new_val| attributes[field] = new_val end end end |
.delete_all ⇒ Object
80 81 82 83 |
# File 'lib/active_hash/base.rb', line 80 def delete_all mark_dirty @records = [] end |
.field(field_name, options = {}) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/active_hash/base.rb', line 113 def field(field_name, = {}) @field_names ||= [] @field_names << field_name define_getter_method(field_name, [:default]) define_setter_method(field_name) define_interrogator_method(field_name) define_custom_find_method(field_name) define_custom_find_all_method(field_name) end |
.fields(*args) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/active_hash/base.rb', line 106 def fields(* args) = args. args.each do |field| field(field, ) end end |
.find(id, *args) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_hash/base.rb', line 85 def find(id, * args) case id when nil nil when :all all when Array all.select { |record| id.map(& :to_i).include?(record.id) } else find_by_id(id) || begin raise RecordNotFound.new("Couldn't find #{name} with ID=#{id}") end end end |
.find_by_id(id) ⇒ Object
100 101 102 |
# File 'lib/active_hash/base.rb', line 100 def find_by_id(id) all.detect { |record| record.id == id.to_i } end |
.insert(record) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/active_hash/base.rb', line 29 def insert(record) @records ||= [] record.attributes[:id] ||= next_id mark_dirty @records << record end |
.mark_clean ⇒ Object
263 264 265 |
# File 'lib/active_hash/base.rb', line 263 def mark_clean self.dirty = false end |
.mark_dirty ⇒ Object
257 258 259 |
# File 'lib/active_hash/base.rb', line 257 def mark_dirty self.dirty = true end |
.method_missing(method_name, *args) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/active_hash/base.rb', line 134 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 } } if config[:all?] matches else result = matches.first if config[:bang?] result || raise(RecordNotFound, "Couldn\'t find #{name} with #{attribute_pairs.collect {|pair| "#{pair.first} = #{pair.second}"}.join(', ')}") else result end end end |
.next_id ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/active_hash/base.rb', line 36 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 |
.reload ⇒ Object
250 251 252 253 |
# File 'lib/active_hash/base.rb', line 250 def reload self.data = read_inheritable_attribute(:data) mark_clean end |
.respond_to?(method_name, include_private = false) ⇒ Boolean
124 125 126 127 128 129 130 131 132 |
# File 'lib/active_hash/base.rb', line 124 def respond_to?(method_name, include_private=false) 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 |
.the_meta_class ⇒ Object
11 12 13 14 15 |
# File 'lib/active_hash/base.rb', line 11 def class << self self end end |
.transaction ⇒ Object
74 75 76 77 78 |
# File 'lib/active_hash/base.rb', line 74 def transaction yield rescue ActiveRecord::Rollback end |
Instance Method Details
#[](key) ⇒ Object
281 282 283 |
# File 'lib/active_hash/base.rb', line 281 def [](key) attributes[key] end |
#[]=(key, val) ⇒ Object
285 286 287 |
# File 'lib/active_hash/base.rb', line 285 def []=(key, val) attributes[key] = val end |
#cache_key ⇒ Object
325 326 327 328 329 330 331 332 333 334 |
# File 'lib/active_hash/base.rb', line 325 def cache_key case when new_record? "#{self.class.model_name.cache_key}/new" when = self[:updated_at] "#{self.class.model_name.cache_key}/#{id}-#{.to_s(:number)}" else "#{self.class.model_name.cache_key}/#{id}" end end |
#destroyed? ⇒ Boolean
303 304 305 |
# File 'lib/active_hash/base.rb', line 303 def destroyed? false end |
#eql?(other) ⇒ Boolean Also known as: ==
315 316 317 |
# File 'lib/active_hash/base.rb', line 315 def eql?(other) other.instance_of?(self.class) and not id.nil? and (id == other.id) end |
#hash ⇒ Object
321 322 323 |
# File 'lib/active_hash/base.rb', line 321 def hash id.hash end |
#id ⇒ Object Also known as: quoted_id
289 290 291 |
# File 'lib/active_hash/base.rb', line 289 def id attributes[:id] ? attributes[:id] : nil end |
#id=(id) ⇒ Object
293 294 295 |
# File 'lib/active_hash/base.rb', line 293 def id=(id) attributes[:id] = id end |
#marked_for_destruction? ⇒ Boolean
347 348 349 |
# File 'lib/active_hash/base.rb', line 347 def marked_for_destruction? false end |
#new_record? ⇒ Boolean
299 300 301 |
# File 'lib/active_hash/base.rb', line 299 def new_record? !self.class.all.include?(self) end |
#readonly? ⇒ Boolean
307 308 309 |
# File 'lib/active_hash/base.rb', line 307 def readonly? true end |
#save ⇒ Object Also known as: save!
336 337 338 339 |
# File 'lib/active_hash/base.rb', line 336 def save self.class.insert(self) true end |
#to_param ⇒ Object
311 312 313 |
# File 'lib/active_hash/base.rb', line 311 def to_param id.to_s end |
#valid? ⇒ Boolean
343 344 345 |
# File 'lib/active_hash/base.rb', line 343 def valid? true end |