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
#delete, #destroy, #destroy!, #dump, #persisted?, #save, #touch, #update, #update!
Methods included from Fields
#read_attribute, #update_attribute, #update_attributes, #write_attribute
Instance Method Details
#==(other) ⇒ Object
An object is equal to another object if their ids are equal.
157 158 159 160 161 162 163 164 |
# File 'lib/dynamoid/document.rb', line 157 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 |
#eql?(other) ⇒ Boolean
166 167 168 |
# File 'lib/dynamoid/document.rb', line 166 def eql?(other) self == other end |
#hash ⇒ Object
170 171 172 |
# File 'lib/dynamoid/document.rb', line 170 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.
190 191 192 |
# File 'lib/dynamoid/document.rb', line 190 def hash_key self.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.
197 198 199 |
# File 'lib/dynamoid/document.rb', line 197 def hash_key=(value) self.send("#{self.class.hash_key}=", value) end |
#initialize(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object.
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/dynamoid/document.rb', line 134 def initialize(attrs = {}) # we need this hack for Rails 4.0 only # because `run_callbacks` calls `attributes` getter while it is still nil @attributes = {} run_callbacks :initialize do @new_record = true @attributes ||= {} @associations ||= {} load(attrs) end end |
#load(attrs) ⇒ Object
148 149 150 151 152 |
# File 'lib/dynamoid/document.rb', line 148 def load(attrs) self.class.undump(attrs).each do |key, value| send("#{key}=", value) if self.respond_to?("#{key}=") end end |
#range_value ⇒ Object
201 202 203 204 205 |
# File 'lib/dynamoid/document.rb', line 201 def range_value if range_key = self.class.range_key self.send(range_key) end end |
#range_value=(value) ⇒ Object
207 208 209 |
# File 'lib/dynamoid/document.rb', line 207 def range_value=(value) self.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.
180 181 182 183 184 185 |
# File 'lib/dynamoid/document.rb', line 180 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 |