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
- .all_in_process ⇒ Object
- .auto_assign_fields(array_of_hashes) ⇒ Object
-
.base_class ⇒ Object
Needed for ActiveRecord polymorphic associations.
- .cache_key ⇒ Object
-
.composite_primary_key? ⇒ Boolean
Needed for ActiveRecord since rails/rails#47664.
- .compute_type(type_name) ⇒ Object
- .configuration_for_custom_finder(finder_name) ⇒ 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?(args = nil) ⇒ Boolean
- .field(field_name, options = {}) ⇒ Object
- .field_names ⇒ Object
- .fields(*args) ⇒ Object
- .has_query_constraints? ⇒ Boolean
- .insert(record) ⇒ Object
- .mark_clean ⇒ Object
- .mark_dirty ⇒ Object
- .method_missing(method_name, *args) ⇒ Object
- .next_id ⇒ Object
- .pluralize_table_names ⇒ Object
-
.polymorphic_name ⇒ Object
Needed for ActiveRecord polymorphic associations(rails/rails#32148).
- .primary_key ⇒ Object
- .record_index ⇒ Object
- .reload ⇒ Object
- .reset_record_index ⇒ Object
- .respond_to?(method_name, include_private = false) ⇒ Boolean
- .scope(name, body) ⇒ Object
- .the_meta_class ⇒ Object
- .transaction ⇒ Object
- .validate_field(field_name) ⇒ Object
- .validate_unique_id(record) ⇒ 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.
396 397 398 399 400 401 402 403 |
# File 'lib/active_hash/base.rb', line 396 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
261 262 263 264 |
# File 'lib/active_hash/base.rb', line 261 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
142 143 144 |
# File 'lib/active_hash/base.rb', line 142 def add_to_record_index(entry) record_index.merge!(entry) end |
.all(options = {}) ⇒ Object
169 170 171 172 173 |
# File 'lib/active_hash/base.rb', line 169 def all( = {}) relation = ActiveHash::Relation.new(self, @records || []) relation = relation.where!([:conditions]) if [:conditions] relation end |
.all_in_process ⇒ Object
121 122 123 |
# File 'lib/active_hash/base.rb', line 121 def all_in_process all end |
.auto_assign_fields(array_of_hashes) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/active_hash/base.rb', line 330 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
347 348 349 |
# File 'lib/active_hash/base.rb', line 347 def base_class ActiveHash::Base end |
.cache_key ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/active_hash/base.rb', line 37 def cache_key if Object.const_defined?(:ActiveModel) model_name.cache_key else ActiveSupport::Inflector.tableize(self.name).downcase end end |
.composite_primary_key? ⇒ Boolean
Needed for ActiveRecord since rails/rails#47664
357 358 359 |
# File 'lib/active_hash/base.rb', line 357 def composite_primary_key? false end |
.compute_type(type_name) ⇒ Object
59 60 61 |
# File 'lib/active_hash/base.rb', line 59 def compute_type(type_name) self end |
.configuration_for_custom_finder(finder_name) ⇒ Object
249 250 251 252 253 254 255 256 257 |
# File 'lib/active_hash/base.rb', line 249 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 |
.create(attributes = {}) ⇒ Object Also known as: add
154 155 156 157 158 159 |
# File 'lib/active_hash/base.rb', line 154 def create(attributes = {}) record = new(attributes) record.save mark_dirty record end |
.create!(attributes = {}) ⇒ Object
163 164 165 166 167 |
# File 'lib/active_hash/base.rb', line 163 def create!(attributes = {}) record = new(attributes) record.save! record end |
.data ⇒ Object
71 72 73 |
# File 'lib/active_hash/base.rb', line 71 def data _data end |
.data=(array_of_hashes) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/active_hash/base.rb', line 75 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
313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/active_hash/base.rb', line 313 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
298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/active_hash/base.rb', line 298 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
266 267 268 269 270 271 272 |
# File 'lib/active_hash/base.rb', line 266 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
287 288 289 290 291 292 293 294 |
# File 'lib/active_hash/base.rb', line 287 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
276 277 278 279 280 281 282 283 |
# File 'lib/active_hash/base.rb', line 276 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
187 188 189 190 191 |
# File 'lib/active_hash/base.rb', line 187 def delete_all mark_dirty reset_record_index @records = [] end |
.empty? ⇒ Boolean
67 68 69 |
# File 'lib/active_hash/base.rb', line 67 def empty? false end |
.exists?(args = nil) ⇒ Boolean
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/active_hash/base.rb', line 88 def exists?(args = nil) if args.respond_to?(:id) record_index[args.id.to_s].present? elsif args == false false elsif args.nil? all.present? elsif args.is_a?(Hash) all.where(args).present? else all.where(id: args.to_i).present? end end |
.field(field_name, options = {}) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/active_hash/base.rb', line 200 def field(field_name, = {}) validate_field(field_name) field_names << field_name add_default_value(field_name, [:default]) if .key?(: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
49 50 51 |
# File 'lib/active_hash/base.rb', line 49 def field_names @field_names ||= [] end |
.fields(*args) ⇒ Object
193 194 195 196 197 198 |
# File 'lib/active_hash/base.rb', line 193 def fields(*args) = args. args.each do |field| field(field, ) end end |
.has_query_constraints? ⇒ Boolean
130 131 132 |
# File 'lib/active_hash/base.rb', line 130 def has_query_constraints? false end |
.insert(record) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/active_hash/base.rb', line 102 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
375 376 377 |
# File 'lib/active_hash/base.rb', line 375 def mark_clean self.dirty = false end |
.mark_dirty ⇒ Object
369 370 371 |
# File 'lib/active_hash/base.rb', line 369 def mark_dirty self.dirty = true end |
.method_missing(method_name, *args) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/active_hash/base.rb', line 230 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
112 113 114 115 116 117 118 119 |
# File 'lib/active_hash/base.rb', line 112 def next_id max_record = all_in_process.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
63 64 65 |
# File 'lib/active_hash/base.rb', line 63 def pluralize_table_names true end |
.polymorphic_name ⇒ Object
Needed for ActiveRecord polymorphic associations(rails/rails#32148)
352 353 354 |
# File 'lib/active_hash/base.rb', line 352 def polymorphic_name base_class.name end |
.primary_key ⇒ Object
45 46 47 |
# File 'lib/active_hash/base.rb', line 45 def primary_key "id" end |
.record_index ⇒ Object
126 127 128 |
# File 'lib/active_hash/base.rb', line 126 def record_index @record_index ||= {} end |
.reload ⇒ Object
361 362 363 364 365 |
# File 'lib/active_hash/base.rb', line 361 def reload reset_record_index self.data = _data mark_clean end |
.reset_record_index ⇒ Object
136 137 138 |
# File 'lib/active_hash/base.rb', line 136 def reset_record_index record_index.clear end |
.respond_to?(method_name, include_private = false) ⇒ Boolean
220 221 222 223 224 225 226 227 228 |
# File 'lib/active_hash/base.rb', line 220 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 |
.scope(name, body) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/active_hash/base.rb', line 381 def scope(name, body) raise ArgumentError, 'body needs to be callable' unless body.respond_to?(:call) self.scopes ||= {} self.scopes[name] = body .instance_eval do define_method(name) do |*args| instance_exec(*args, &body) end end end |
.the_meta_class ⇒ Object
53 54 55 56 57 |
# File 'lib/active_hash/base.rb', line 53 def class << self self end end |
.transaction ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'lib/active_hash/base.rb', line 177 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
212 213 214 215 216 |
# File 'lib/active_hash/base.rb', line 212 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
148 149 150 |
# File 'lib/active_hash/base.rb', line 148 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 |
Instance Method Details
#[](key) ⇒ Object
413 414 415 |
# File 'lib/active_hash/base.rb', line 413 def [](key) attributes[key] end |
#[]=(key, val) ⇒ Object
422 423 424 |
# File 'lib/active_hash/base.rb', line 422 def []=(key, val) @attributes[key] = val end |
#_read_attribute(key) ⇒ Object Also known as: read_attribute
417 418 419 |
# File 'lib/active_hash/base.rb', line 417 def _read_attribute(key) attributes[key.to_sym] end |
#attributes ⇒ Object
405 406 407 408 409 410 411 |
# File 'lib/active_hash/base.rb', line 405 def attributes if self.class.default_attributes (self.class.default_attributes.merge @attributes).freeze else @attributes end end |
#cache_key ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/active_hash/base.rb', line 462 def cache_key case when new_record? "#{self.class.cache_key}/new" when = self[:updated_at] if ActiveSupport::VERSION::MAJOR < 7 "#{self.class.cache_key}/#{id}-#{.to_s(:number)}" else "#{self.class.cache_key}/#{id}-#{.to_fs(:number)}" end else "#{self.class.cache_key}/#{id}" end end |
#destroyed? ⇒ Boolean
440 441 442 |
# File 'lib/active_hash/base.rb', line 440 def destroyed? false end |
#eql?(other) ⇒ Boolean Also known as: ==
452 453 454 |
# File 'lib/active_hash/base.rb', line 452 def eql?(other) other.instance_of?(self.class) and not id.nil? and (id == other.id) end |
#errors ⇒ Object
477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/active_hash/base.rb', line 477 def errors obj = Object.new def obj.[](key) [] end def obj.() [] end obj end |
#hash ⇒ Object
458 459 460 |
# File 'lib/active_hash/base.rb', line 458 def hash id.hash end |
#id ⇒ Object Also known as: quoted_id
426 427 428 |
# File 'lib/active_hash/base.rb', line 426 def id attributes[:id] ? attributes[:id] : nil end |
#id=(id) ⇒ Object
430 431 432 |
# File 'lib/active_hash/base.rb', line 430 def id=(id) @attributes[:id] = id end |
#marked_for_destruction? ⇒ Boolean
504 505 506 |
# File 'lib/active_hash/base.rb', line 504 def marked_for_destruction? false end |
#new_record? ⇒ Boolean
436 437 438 |
# File 'lib/active_hash/base.rb', line 436 def new_record? !self.class.all.include?(self) end |
#persisted? ⇒ Boolean
444 445 446 |
# File 'lib/active_hash/base.rb', line 444 def persisted? self.class.all.map(&:id).include?(id) end |
#readonly? ⇒ Boolean
448 449 450 |
# File 'lib/active_hash/base.rb', line 448 def readonly? true end |
#save(*args) ⇒ Object Also known as: save!
491 492 493 494 495 496 |
# File 'lib/active_hash/base.rb', line 491 def save(*args) unless self.class.exists?(self) self.class.insert(self) end true end |
#to_param ⇒ Object
30 31 32 |
# File 'lib/active_hash/base.rb', line 30 def to_param id.present? ? id.to_s : nil end |
#valid? ⇒ Boolean
500 501 502 |
# File 'lib/active_hash/base.rb', line 500 def valid? true end |