Module: Dynamoid::Document
Overview
This is the base module for all domain objects that need to be persisted to the database as documents.
Defined Under Namespace
Modules: ClassMethods
Constant Summary
Constants included from Finders
Constants included from Persistence
Constants included from Fields
Instance Attribute Summary
Attributes included from Persistence
Attributes included from Fields
Instance Method Summary collapse
-
#==(other) ⇒ Object
An object is equal to another object if their ids are equal.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#hash_key ⇒ Object
Return an object’s hash key, regardless of what it might be called to the object.
-
#hash_key=(value) ⇒ Object
Assign an object’s hash key, regardless of what it might be called to the object.
-
#initialize(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object.
- #load(attrs) ⇒ Object
- #range_value ⇒ Object
- #range_value=(value) ⇒ Object
-
#reload ⇒ Dynamoid::Document
Reload an object from the database – if you suspect the object has changed in the datastore and you need those changes to be reflected immediately, you would call this method.
Methods included from Dirty
#clear_changes, #save, #update!, #write_attribute
Methods included from IdentityMap
clear, #delete, #identity_map, #identity_map_key, #save
Methods included from Validations
Methods included from Persistence
#decrement, #decrement!, #delete, #destroy, #destroy!, #increment, #increment!, #persisted?, #save, #touch, #update, #update!, #update_attribute, #update_attributes, #update_attributes!
Methods included from Fields
#attributes_before_type_cast, #read_attribute, #read_attribute_before_type_cast, #write_attribute
Instance Method Details
#==(other) ⇒ Object
An object is equal to another object if their ids are equal.
350 351 352 353 354 355 356 357 358 |
# File 'lib/dynamoid/document.rb', line 350 def ==(other) if self.class.identity_map_on? super else return false if other.nil? other.is_a?(Dynamoid::Document) && hash_key == other.hash_key && range_value == other.range_value end end |
#eql?(other) ⇒ Boolean
360 361 362 |
# File 'lib/dynamoid/document.rb', line 360 def eql?(other) self == other end |
#hash ⇒ Object
364 365 366 |
# File 'lib/dynamoid/document.rb', line 364 def hash hash_key.hash ^ range_value.hash end |
#hash_key ⇒ Object
Return an object’s hash key, regardless of what it might be called to the object.
389 390 391 |
# File 'lib/dynamoid/document.rb', line 389 def hash_key send(self.class.hash_key) end |
#hash_key=(value) ⇒ Object
Assign an object’s hash key, regardless of what it might be called to the object.
396 397 398 |
# File 'lib/dynamoid/document.rb', line 396 def hash_key=(value) send("#{self.class.hash_key}=", value) end |
#initialize(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/dynamoid/document.rb', line 319 def initialize(attrs = {}) run_callbacks :initialize do @new_record = true @attributes ||= {} @associations ||= {} @attributes_before_type_cast ||= {} attrs_with_defaults = {} self.class.attributes.each do |attribute, | attrs_with_defaults[attribute] = if attrs.key?(attribute) attrs[attribute] elsif .key?(:default) evaluate_default_value([:default]) end end attrs_virtual = attrs.slice(*(attrs.keys - self.class.attributes.keys)) load(attrs_with_defaults.merge(attrs_virtual)) end end |
#load(attrs) ⇒ Object
341 342 343 344 345 |
# File 'lib/dynamoid/document.rb', line 341 def load(attrs) attrs.each do |key, value| send("#{key}=", value) if respond_to?("#{key}=") end end |
#range_value ⇒ Object
400 401 402 403 404 |
# File 'lib/dynamoid/document.rb', line 400 def range_value if range_key = self.class.range_key send(range_key) end end |
#range_value=(value) ⇒ Object
406 407 408 |
# File 'lib/dynamoid/document.rb', line 406 def range_value=(value) send("#{self.class.range_key}=", value) end |
#reload ⇒ Dynamoid::Document
Reload an object from the database – if you suspect the object has changed in the datastore and you need those changes to be reflected immediately, you would call this method. This is a consistent read.
374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/dynamoid/document.rb', line 374 def reload = { consistent_read: true } if self.class.range_key [:range_key] = range_value end self.attributes = self.class.find(hash_key, ).attributes @associations.values.each(&:reset) self end |