Class: ActiveHash::Base
- Inherits:
-
Object
- Object
- ActiveHash::Base
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion
- Defined in:
- lib/active_hash/base.rb
Direct Known Subclasses
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.
- .compute_type(type_name) ⇒ Object
- .configuration_for_custom_finder(finder_name) ⇒ Object
- .count ⇒ Object
- .create(attributes = {}) ⇒ Object (also: add)
- .create!(attributes = {}) ⇒ Object
- .data ⇒ 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
- .field_names ⇒ Object
- .fields(*args) ⇒ Object
- .find(id, *args) ⇒ Object
- .find_by_id(id) ⇒ Object
- .has_instance_method?(name) ⇒ Boolean
- .has_singleton_method?(name) ⇒ Boolean
- .insert(record) ⇒ Object
- .mark_clean ⇒ Object
- .mark_dirty ⇒ Object
- .method_missing(method_name, *args) ⇒ Object
- .next_id ⇒ Object
- .pluralize_table_names ⇒ Object
- .primary_key ⇒ Object
- .reload ⇒ Object
- .respond_to?(method_name, include_private = false) ⇒ Boolean
- .the_meta_class ⇒ Object
- .transaction ⇒ Object
- .validate_field(field_name) ⇒ Object
- .where(options) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #cache_key ⇒ Object
- #destroyed? ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #errors ⇒ Object
- #hash ⇒ Object
- #id ⇒ Object (also: #quoted_id)
- #id=(id) ⇒ Object
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
- #marked_for_destruction? ⇒ Boolean
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #readonly? ⇒ Boolean
- #save(*args) ⇒ Object (also: #save!)
- #to_param ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
335 336 337 338 339 340 341 |
# File 'lib/active_hash/base.rb', line 335 def initialize(attributes = {}) attributes.symbolize_keys! @attributes = attributes attributes.dup.each do |key, value| send "#{key}=", value end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
333 334 335 |
# File 'lib/active_hash/base.rb', line 333 def attributes @attributes end |
Class Method Details
.all(options = {}) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/active_hash/base.rb', line 97 def all(={}) if .has_key?(:conditions) where([:conditions]) else @records || [] end end |
.auto_assign_fields(array_of_hashes) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/active_hash/base.rb', line 279 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
296 297 298 |
# File 'lib/active_hash/base.rb', line 296 def base_class ActiveHash::Base end |
.compute_type(type_name) ⇒ Object
42 43 44 |
# File 'lib/active_hash/base.rb', line 42 def compute_type(type_name) self end |
.configuration_for_custom_finder(finder_name) ⇒ Object
203 204 205 206 207 208 209 210 211 |
# File 'lib/active_hash/base.rb', line 203 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
111 112 113 |
# File 'lib/active_hash/base.rb', line 111 def count all.length end |
.create(attributes = {}) ⇒ Object Also known as: add
82 83 84 85 86 87 |
# File 'lib/active_hash/base.rb', line 82 def create(attributes = {}) record = new(attributes) record.save mark_dirty record end |
.create!(attributes = {}) ⇒ Object
91 92 93 94 95 |
# File 'lib/active_hash/base.rb', line 91 def create!(attributes = {}) record = new(attributes) record.save! record end |
.data ⇒ Object
50 51 52 |
# File 'lib/active_hash/base.rb', line 50 def data _data end |
.data=(array_of_hashes) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_hash/base.rb', line 54 def data=(array_of_hashes) mark_dirty @records = nil self._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
262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/active_hash/base.rb', line 262 def define_custom_find_all_method(field_name) method_name = :"find_all_by_#{field_name}" unless has_singleton_method?(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
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/active_hash/base.rb', line 247 def define_custom_find_method(field_name) method_name = :"find_by_#{field_name}" unless has_singleton_method?(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
215 216 217 218 219 220 221 |
# File 'lib/active_hash/base.rb', line 215 def define_getter_method(field, default_value) unless has_instance_method?(field) define_method(field) do attributes[field].nil? ? default_value : attributes[field] end end end |
.define_interrogator_method(field) ⇒ Object
236 237 238 239 240 241 242 243 |
# File 'lib/active_hash/base.rb', line 236 def define_interrogator_method(field) method_name = :"#{field}?" unless has_instance_method?(method_name) define_method(method_name) do send(field).present? end end end |
.define_setter_method(field) ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'lib/active_hash/base.rb', line 225 def define_setter_method(field) method_name = "#{field}=" unless has_instance_method?(method_name) define_method(method_name) do |new_val| attributes[field] = new_val end end end |
.delete_all ⇒ Object
122 123 124 125 |
# File 'lib/active_hash/base.rb', line 122 def delete_all mark_dirty @records = [] end |
.field(field_name, options = {}) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/active_hash/base.rb', line 155 def field(field_name, = {}) validate_field(field_name) 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 |
.field_names ⇒ Object
32 33 34 |
# File 'lib/active_hash/base.rb', line 32 def field_names @field_names ||= [] end |
.fields(*args) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/active_hash/base.rb', line 148 def fields(*args) = args. args.each do |field| field(field, ) end end |
.find(id, *args) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/active_hash/base.rb', line 127 def find(id, * args) case id when nil nil when :all all when Array id.map { |i| find(i) } else find_by_id(id) || begin raise RecordNotFound.new("Couldn't find #{name} with ID=#{id}") end end end |
.find_by_id(id) ⇒ Object
142 143 144 |
# File 'lib/active_hash/base.rb', line 142 def find_by_id(id) all.detect { |record| record.id.to_s == id.to_s } end |
.has_instance_method?(name) ⇒ Boolean
319 320 321 |
# File 'lib/active_hash/base.rb', line 319 def has_instance_method?(name) instance_methods.map { |method| method.to_sym }.include?(name) end |
.has_singleton_method?(name) ⇒ Boolean
325 326 327 |
# File 'lib/active_hash/base.rb', line 325 def has_singleton_method?(name) singleton_methods.map { |method| method.to_sym }.include?(name) end |
.insert(record) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/active_hash/base.rb', line 66 def insert(record) @records ||= [] record.attributes[:id] ||= next_id mark_dirty @records << record end |
.mark_clean ⇒ Object
313 314 315 |
# File 'lib/active_hash/base.rb', line 313 def mark_clean self.dirty = false end |
.mark_dirty ⇒ Object
307 308 309 |
# File 'lib/active_hash/base.rb', line 307 def mark_dirty self.dirty = true end |
.method_missing(method_name, *args) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/active_hash/base.rb', line 184 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[0]} = #{pair[1]}" }.join(', ')}") else result end end end |
.next_id ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/active_hash/base.rb', line 73 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 |
.pluralize_table_names ⇒ Object
46 47 48 |
# File 'lib/active_hash/base.rb', line 46 def pluralize_table_names true end |
.primary_key ⇒ Object
28 29 30 |
# File 'lib/active_hash/base.rb', line 28 def primary_key "id" end |
.reload ⇒ Object
300 301 302 303 |
# File 'lib/active_hash/base.rb', line 300 def reload self.data = _data mark_clean end |
.respond_to?(method_name, include_private = false) ⇒ Boolean
174 175 176 177 178 179 180 181 182 |
# File 'lib/active_hash/base.rb', line 174 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
36 37 38 39 40 |
# File 'lib/active_hash/base.rb', line 36 def class << self self end end |
.transaction ⇒ Object
115 116 117 118 119 120 |
# File 'lib/active_hash/base.rb', line 115 def transaction yield rescue LocalJumpError => err raise err rescue ActiveRecord::Rollback end |
.validate_field(field_name) ⇒ Object
166 167 168 169 170 |
# File 'lib/active_hash/base.rb', line 166 def validate_field(field_name) if [:attributes].include?(field_name.to_sym) raise ReservedFieldError.new("#{field_name} is a reserved field in ActiveHash. Please use another name.") end end |
.where(options) ⇒ Object
105 106 107 108 109 |
# File 'lib/active_hash/base.rb', line 105 def where() (@records || []).select do |record| .all? { |col, match| record[col] == match } end end |
Instance Method Details
#[](key) ⇒ Object
343 344 345 |
# File 'lib/active_hash/base.rb', line 343 def [](key) attributes[key] end |
#[]=(key, val) ⇒ Object
347 348 349 |
# File 'lib/active_hash/base.rb', line 347 def []=(key, val) attributes[key] = val end |
#cache_key ⇒ Object
387 388 389 390 391 392 393 394 395 396 |
# File 'lib/active_hash/base.rb', line 387 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
365 366 367 |
# File 'lib/active_hash/base.rb', line 365 def destroyed? false end |
#eql?(other) ⇒ Boolean Also known as: ==
377 378 379 |
# File 'lib/active_hash/base.rb', line 377 def eql?(other) other.instance_of?(self.class) and not id.nil? and (id == other.id) end |
#errors ⇒ Object
398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/active_hash/base.rb', line 398 def errors obj = Object.new def obj.[](key) [] end def obj.() [] end obj end |
#hash ⇒ Object
383 384 385 |
# File 'lib/active_hash/base.rb', line 383 def hash id.hash end |
#id ⇒ Object Also known as: quoted_id
351 352 353 |
# File 'lib/active_hash/base.rb', line 351 def id attributes[:id] ? attributes[:id] : nil end |
#id=(id) ⇒ Object
355 356 357 |
# File 'lib/active_hash/base.rb', line 355 def id=(id) attributes[:id] = id end |
#marked_for_destruction? ⇒ Boolean
423 424 425 |
# File 'lib/active_hash/base.rb', line 423 def marked_for_destruction? false end |
#new_record? ⇒ Boolean
361 362 363 |
# File 'lib/active_hash/base.rb', line 361 def new_record? !self.class.all.include?(self) end |
#persisted? ⇒ Boolean
369 370 371 |
# File 'lib/active_hash/base.rb', line 369 def persisted? self.class.all.map(&:id).include?(id) end |
#readonly? ⇒ Boolean
373 374 375 |
# File 'lib/active_hash/base.rb', line 373 def readonly? true end |
#save(*args) ⇒ Object Also known as: save!
412 413 414 415 |
# File 'lib/active_hash/base.rb', line 412 def save(*args) self.class.insert(self) true end |
#to_param ⇒ Object
21 22 23 |
# File 'lib/active_hash/base.rb', line 21 def to_param id.present? ? id.to_s : nil end |
#valid? ⇒ Boolean
419 420 421 |
# File 'lib/active_hash/base.rb', line 419 def valid? true end |