module Dynamoid::Document
This is the base module for all domain objects that need to be persisted to the database as documents.
Public Class Methods
Initialize a new object.
@param [Hash] attrs Attributes with which to create the object.
@return [Dynamoid::Document] the new document
@since 0.2.0
# File lib/dynamoid/document.rb, line 122 def initialize(attrs = {}) run_callbacks :initialize do @new_record = true @attributes ||= {} @associations ||= {} load(attrs) end end
Public Instance Methods
An object is equal to another object if their ids are equal.
@since 0.2.0
# File lib/dynamoid/document.rb, line 141 def ==(other) if self.class.identity_map_on? super else return false if other.nil? other.is_a?(Dynamoid::Document) && self.hash_key == other.hash_key && self.range_value == other.range_value end end
# File lib/dynamoid/document.rb, line 150 def eql?(other) self == other end
# File lib/dynamoid/document.rb, line 154 def hash hash_key.hash ^ range_value.hash end
Return an object’s hash key, regardless of what it might be called to the object.
@since 0.4.0
# File lib/dynamoid/document.rb, line 174 def hash_key self.send(self.class.hash_key) end
Assign an object’s hash key, regardless of what it might be called to the object.
@since 0.4.0
# File lib/dynamoid/document.rb, line 181 def hash_key=(value) self.send("#{self.class.hash_key}=", value) end
# File lib/dynamoid/document.rb, line 132 def load(attrs) self.class.undump(attrs).each do |key, value| send("#{key}=", value) if self.respond_to?("#{key}=") end end
# File lib/dynamoid/document.rb, line 185 def range_value if range_key = self.class.range_key self.send(range_key) end end
# File lib/dynamoid/document.rb, line 191 def range_value=(value) self.send("#{self.class.range_key}=", value) end
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.
@return [Dynamoid::Document] the document this method was called on
@since 0.2.0
# File lib/dynamoid/document.rb, line 164 def reload range_key_value = range_value ? dumped_range_value : nil self.attributes = self.class.find(hash_key, :range_key => range_key_value, :consistent_read => true).attributes @associations.values.each(&:reset) self end
Private Instance Methods
# File lib/dynamoid/document.rb, line 197 def dumped_range_value dump_field(range_value, self.class.attributes[self.class.range_key]) end