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
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.
-
#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, #write_attribute
Methods included from IdentityMap
clear, #delete, #identity_map, #identity_map_key, models, #save
Methods included from Validations
Methods included from Persistence
#delete, #destroy, #dump, #persisted?, #save, #touch, #update, #update!
Methods included from Indexes
#delete_indexes, #save_indexes
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.
129 130 131 132 133 134 135 136 |
# File 'lib/dynamoid/document.rb', line 129 def ==(other) if self.class.identity_map_on? super else return false if other.nil? other.respond_to?(:hash_key) && other.hash_key == self.hash_key end end |
#hash_key ⇒ Object
Return an object’s hash key, regardless of what it might be called to the object.
153 154 155 |
# File 'lib/dynamoid/document.rb', line 153 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.
160 161 162 |
# File 'lib/dynamoid/document.rb', line 160 def hash_key=(value) self.send("#{self.class.hash_key}=", value) end |
#initialize(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/dynamoid/document.rb', line 110 def initialize(attrs = {}) run_callbacks :initialize do self.class.send(:field, self.class.hash_key) unless self.respond_to?(self.class.hash_key) @new_record = true @attributes ||= {} @associations ||= {} load(attrs) end end |
#load(attrs) ⇒ Object
122 123 124 |
# File 'lib/dynamoid/document.rb', line 122 def load(attrs) self.class.undump(attrs).each {|key, value| send "#{key}=", value } end |
#range_value ⇒ Object
164 165 166 167 168 |
# File 'lib/dynamoid/document.rb', line 164 def range_value if range_key = self.class.range_key self.send(range_key) end end |
#range_value=(value) ⇒ Object
170 171 172 |
# File 'lib/dynamoid/document.rb', line 170 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.
144 145 146 147 148 |
# File 'lib/dynamoid/document.rb', line 144 def reload self.attributes = self.class.find(hash_key, :range_key => range_value).attributes @associations.values.each(&:reset) self end |