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
Class Method Summary collapse
- .add_default_value(field_name, default_value) ⇒ Object
- .add_to_record_index(entry) ⇒ Object
- .all(options = {}) ⇒ Object
- .auto_assign_fields(array_of_hashes) ⇒ Object
-
.base_class ⇒ Object
Needed for ActiveRecord polymorphic associations.
- .cache_key ⇒ Object
- .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
- .empty? ⇒ Boolean
- .exists?(record) ⇒ Boolean
- .field(field_name, options = {}) ⇒ Object
- .field_names ⇒ Object
- .fields(*args) ⇒ Object
- .find(id, *args) ⇒ Object
- .find_by(options) ⇒ Object
- .find_by!(options) ⇒ Object
- .find_by_id(id) ⇒ Object
- .insert(record) ⇒ Object
- .mark_clean ⇒ Object
- .mark_dirty ⇒ Object
- .match_options?(record, options) ⇒ Boolean
- .method_missing(method_name, *args) ⇒ Object
- .next_id ⇒ Object
- .pluralize_table_names ⇒ Object
- .primary_key ⇒ Object
- .record_index ⇒ Object
- .reload ⇒ Object
- .reset_record_index ⇒ Object
- .respond_to?(method_name, include_private = false) ⇒ Boolean
- .the_meta_class ⇒ Object
- .transaction ⇒ Object
- .validate_field(field_name) ⇒ Object
- .validate_unique_id(record) ⇒ Object
- .where(options) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #_read_attribute(key) ⇒ Object (also: #read_attribute)
- #attributes ⇒ Object
- #cache_key ⇒ Object
- #destroyed? ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #errors ⇒ Object
- #hash ⇒ Object
- #id ⇒ Object (also: #quoted_id)
- #id=(id) ⇒ Object
-
#initialize(attributes = {}) {|_self| ... } ⇒ 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 = {}) {|_self| ... } ⇒ Base
Returns a new instance of Base.
416 417 418 419 420 421 422 423 |
# File 'lib/active_hash/base.rb', line 416 def initialize(attributes = {}) attributes.symbolize_keys! @attributes = attributes attributes.dup.each do |key, value| send "#{key}=", value end yield self if block_given? end |
Class Method Details
.add_default_value(field_name, default_value) ⇒ Object
304 305 306 307 |
# File 'lib/active_hash/base.rb', line 304 def add_default_value field_name, default_value self.default_attributes ||= {} self.default_attributes[field_name] = default_value end |
.add_to_record_index(entry) ⇒ Object
122 123 124 |
# File 'lib/active_hash/base.rb', line 122 def add_to_record_index(entry) record_index.merge!(entry) end |
.all(options = {}) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/active_hash/base.rb', line 149 def all(={}) if .has_key?(:conditions) where([:conditions]) else @records ||= [] end end |
.auto_assign_fields(array_of_hashes) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/active_hash/base.rb', line 373 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
390 391 392 |
# File 'lib/active_hash/base.rb', line 390 def base_class ActiveHash::Base end |
.cache_key ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/active_hash/base.rb', line 34 def cache_key if Object.const_defined?(:ActiveModel) model_name.cache_key else ActiveSupport::Inflector.tableize(self.name).downcase end end |
.compute_type(type_name) ⇒ Object
56 57 58 |
# File 'lib/active_hash/base.rb', line 56 def compute_type(type_name) self end |
.configuration_for_custom_finder(finder_name) ⇒ Object
292 293 294 295 296 297 298 299 300 |
# File 'lib/active_hash/base.rb', line 292 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
192 193 194 |
# File 'lib/active_hash/base.rb', line 192 def count all.length end |
.create(attributes = {}) ⇒ Object Also known as: add
134 135 136 137 138 139 |
# File 'lib/active_hash/base.rb', line 134 def create(attributes = {}) record = new(attributes) record.save mark_dirty record end |
.create!(attributes = {}) ⇒ Object
143 144 145 146 147 |
# File 'lib/active_hash/base.rb', line 143 def create!(attributes = {}) record = new(attributes) record.save! record end |
.data ⇒ Object
68 69 70 |
# File 'lib/active_hash/base.rb', line 68 def data _data end |
.data=(array_of_hashes) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/active_hash/base.rb', line 72 def data=(array_of_hashes) mark_dirty @records = nil reset_record_index 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
356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/active_hash/base.rb', line 356 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
341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/active_hash/base.rb', line 341 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
309 310 311 312 313 314 315 |
# File 'lib/active_hash/base.rb', line 309 def define_getter_method(field, default_value) unless instance_methods.include?(field.to_sym) define_method(field) do attributes[field].nil? ? default_value : attributes[field] end end end |
.define_interrogator_method(field) ⇒ Object
330 331 332 333 334 335 336 337 |
# File 'lib/active_hash/base.rb', line 330 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
319 320 321 322 323 324 325 326 |
# File 'lib/active_hash/base.rb', line 319 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
206 207 208 209 210 |
# File 'lib/active_hash/base.rb', line 206 def delete_all mark_dirty reset_record_index @records = [] end |
.empty? ⇒ Boolean
64 65 66 |
# File 'lib/active_hash/base.rb', line 64 def empty? false end |
.exists?(record) ⇒ Boolean
85 86 87 88 89 |
# File 'lib/active_hash/base.rb', line 85 def exists?(record) if record.id.present? record_index[record.id.to_s].present? end end |
.field(field_name, options = {}) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/active_hash/base.rb', line 243 def field(field_name, = {}) validate_field(field_name) field_names << field_name add_default_value(field_name, [:default]) if [:default] 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
46 47 48 |
# File 'lib/active_hash/base.rb', line 46 def field_names @field_names ||= [] end |
.fields(*args) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/active_hash/base.rb', line 236 def fields(*args) = args. args.each do |field| field(field, ) end end |
.find(id, *args) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/active_hash/base.rb', line 212 def find(id, * args) case id when nil nil when :all all when :first all(*args).first 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(options) ⇒ Object
172 173 174 |
# File 'lib/active_hash/base.rb', line 172 def find_by() where().first end |
.find_by!(options) ⇒ Object
176 177 178 |
# File 'lib/active_hash/base.rb', line 176 def find_by!() find_by() || (raise RecordNotFound.new("Couldn't find #{name}")) end |
.find_by_id(id) ⇒ Object
229 230 231 232 |
# File 'lib/active_hash/base.rb', line 229 def find_by_id(id) index = record_index[id.to_s] index and @records[index] end |
.insert(record) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/active_hash/base.rb', line 91 def insert(record) @records ||= [] record[:id] ||= next_id validate_unique_id(record) if dirty mark_dirty add_to_record_index({ record.id.to_s => @records.length }) @records << record end |
.mark_clean ⇒ Object
408 409 410 |
# File 'lib/active_hash/base.rb', line 408 def mark_clean self.dirty = false end |
.mark_dirty ⇒ Object
402 403 404 |
# File 'lib/active_hash/base.rb', line 402 def mark_dirty self.dirty = true end |
.match_options?(record, options) ⇒ Boolean
180 181 182 183 184 185 186 187 188 |
# File 'lib/active_hash/base.rb', line 180 def (record, ) .all? do |col, match| if match.kind_of?(Array) match.include?(record[col]) else record[col] == match end end end |
.method_missing(method_name, *args) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/active_hash/base.rb', line 273 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
101 102 103 104 105 106 107 108 |
# File 'lib/active_hash/base.rb', line 101 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
60 61 62 |
# File 'lib/active_hash/base.rb', line 60 def pluralize_table_names true end |
.primary_key ⇒ Object
42 43 44 |
# File 'lib/active_hash/base.rb', line 42 def primary_key "id" end |
.record_index ⇒ Object
110 111 112 |
# File 'lib/active_hash/base.rb', line 110 def record_index @record_index ||= {} end |
.reload ⇒ Object
394 395 396 397 398 |
# File 'lib/active_hash/base.rb', line 394 def reload reset_record_index self.data = _data mark_clean end |
.reset_record_index ⇒ Object
116 117 118 |
# File 'lib/active_hash/base.rb', line 116 def reset_record_index record_index.clear end |
.respond_to?(method_name, include_private = false) ⇒ Boolean
263 264 265 266 267 268 269 270 271 |
# File 'lib/active_hash/base.rb', line 263 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
50 51 52 53 54 |
# File 'lib/active_hash/base.rb', line 50 def class << self self end end |
.transaction ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/active_hash/base.rb', line 196 def transaction yield rescue LocalJumpError => err raise err rescue StandardError => e unless Object.const_defined?(:ActiveRecord) && e.is_a?(ActiveRecord::Rollback) raise e end end |
.validate_field(field_name) ⇒ Object
255 256 257 258 259 |
# File 'lib/active_hash/base.rb', line 255 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 |
.validate_unique_id(record) ⇒ Object
128 129 130 |
# File 'lib/active_hash/base.rb', line 128 def validate_unique_id(record) raise IdError.new("Duplicate ID found for record #{record.attributes.inspect}") if record_index.has_key?(record.id.to_s) end |
.where(options) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/active_hash/base.rb', line 157 def where() return @records if .blank? # use index if searching by id if .key?(:id) || .key?("id") ids = (.delete(:id) || .delete("id")) candidates = Array.wrap(ids).map { |id| find_by_id(id) }.compact end return candidates if .blank? (candidates || @records || []).select do |record| (record, ) end end |
Instance Method Details
#[](key) ⇒ Object
433 434 435 |
# File 'lib/active_hash/base.rb', line 433 def [](key) attributes[key] end |
#[]=(key, val) ⇒ Object
442 443 444 |
# File 'lib/active_hash/base.rb', line 442 def []=(key, val) @attributes[key] = val end |
#_read_attribute(key) ⇒ Object Also known as: read_attribute
437 438 439 |
# File 'lib/active_hash/base.rb', line 437 def _read_attribute(key) attributes[key] end |
#attributes ⇒ Object
425 426 427 428 429 430 431 |
# File 'lib/active_hash/base.rb', line 425 def attributes if self.class.default_attributes (self.class.default_attributes.merge @attributes).freeze else @attributes end end |
#cache_key ⇒ Object
482 483 484 485 486 487 488 489 490 491 |
# File 'lib/active_hash/base.rb', line 482 def cache_key case when new_record? "#{self.class.cache_key}/new" when = self[:updated_at] "#{self.class.cache_key}/#{id}-#{.to_s(:number)}" else "#{self.class.cache_key}/#{id}" end end |
#destroyed? ⇒ Boolean
460 461 462 |
# File 'lib/active_hash/base.rb', line 460 def destroyed? false end |
#eql?(other) ⇒ Boolean Also known as: ==
472 473 474 |
# File 'lib/active_hash/base.rb', line 472 def eql?(other) other.instance_of?(self.class) and not id.nil? and (id == other.id) end |
#errors ⇒ Object
493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/active_hash/base.rb', line 493 def errors obj = Object.new def obj.[](key) [] end def obj.() [] end obj end |
#hash ⇒ Object
478 479 480 |
# File 'lib/active_hash/base.rb', line 478 def hash id.hash end |
#id ⇒ Object Also known as: quoted_id
446 447 448 |
# File 'lib/active_hash/base.rb', line 446 def id attributes[:id] ? attributes[:id] : nil end |
#id=(id) ⇒ Object
450 451 452 |
# File 'lib/active_hash/base.rb', line 450 def id=(id) @attributes[:id] = id end |
#marked_for_destruction? ⇒ Boolean
520 521 522 |
# File 'lib/active_hash/base.rb', line 520 def marked_for_destruction? false end |
#new_record? ⇒ Boolean
456 457 458 |
# File 'lib/active_hash/base.rb', line 456 def new_record? !self.class.all.include?(self) end |
#persisted? ⇒ Boolean
464 465 466 |
# File 'lib/active_hash/base.rb', line 464 def persisted? self.class.all.map(&:id).include?(id) end |
#readonly? ⇒ Boolean
468 469 470 |
# File 'lib/active_hash/base.rb', line 468 def readonly? true end |
#save(*args) ⇒ Object Also known as: save!
507 508 509 510 511 512 |
# File 'lib/active_hash/base.rb', line 507 def save(*args) unless self.class.exists?(self) self.class.insert(self) end true end |
#to_param ⇒ Object
27 28 29 |
# File 'lib/active_hash/base.rb', line 27 def to_param id.present? ? id.to_s : nil end |
#valid? ⇒ Boolean
516 517 518 |
# File 'lib/active_hash/base.rb', line 516 def valid? true end |