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 Indexes
Indexes::PERMITTED_KEY_DYNAMODB_TYPES
Constants included from Persistence
Instance Attribute Summary
Attributes included from Fields
Attributes included from Persistence
Instance Method Summary collapse
-
#==(other) ⇒ true|false
Check equality of two models.
-
#eql?(other) ⇒ true|false
Check equality of two models.
-
#hash ⇒ Integer
Generate an Integer hash value for this model.
-
#hash_key ⇒ Object
Return a model’s hash key value.
-
#hash_key=(value) ⇒ Object
Assign a model’s hash key value, regardless of what it might be called to the object.
-
#initialize(attrs = {}, &block) ⇒ Dynamoid::Document
Initialize a new object.
-
#range_value ⇒ Object
Return a model’s range key value.
-
#range_value=(value) ⇒ Object
Assign a model’s range key value.
Methods included from IdentityMap
clear, #delete, #identity_map, #identity_map_key, #save
Methods included from Validations
#save, #save!, #update_attribute, #valid?
Methods included from Fields
#attributes_before_type_cast, #read_attribute, #read_attribute_before_type_cast, #write_attribute
Methods included from Dirty
#attribute_changed?, #attribute_previous_change, #attribute_previously_changed?, #attribute_was, #changed, #changed?, #changed_attributes, #changes, #previous_changes, #reload, #restore_attributes, #save, #save!, #update, #update!
Methods included from Loadable
Methods included from Persistence
#decrement, #decrement!, #delete, #destroy, #destroy!, #increment, #increment!, #persisted?, #save, #touch, #update, #update!, #update_attribute, #update_attributes, #update_attributes!
Instance Method Details
#==(other) ⇒ true|false
Check equality of two models.
A model is equal to another model only if their primary keys (hash key and optionally range key) are equal.
221 222 223 224 225 226 227 228 229 |
# File 'lib/dynamoid/document.rb', line 221 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) ⇒ true|false
Check equality of two models.
Works exactly like == does.
236 237 238 |
# File 'lib/dynamoid/document.rb', line 236 def eql?(other) self == other end |
#hash ⇒ Integer
Generate an Integer hash value for this model.
Hash value is based on primary key. So models can be used safely as a Hash
keys.
246 247 248 |
# File 'lib/dynamoid/document.rb', line 246 def hash hash_key.hash ^ range_value.hash end |
#hash_key ⇒ Object
Return a model’s hash key value.
253 254 255 |
# File 'lib/dynamoid/document.rb', line 253 def hash_key self[self.class.hash_key.to_sym] end |
#hash_key=(value) ⇒ Object
Assign a model’s hash key value, regardless of what it might be called to the object.
261 262 263 |
# File 'lib/dynamoid/document.rb', line 261 def hash_key=(value) self[self.class.hash_key.to_sym] = value end |
#initialize(attrs = {}, &block) ⇒ Dynamoid::Document
Initialize a new object.
User.new(name: 'A')
Initialize an object and pass it into a block to set other attributes.
User.new(name: 'A') do |u|
u.age = 21
end
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/dynamoid/document.rb', line 189 def initialize(attrs = {}, &block) run_callbacks :initialize do @new_record = true @attributes ||= {} @associations ||= {} @attributes_before_type_cast ||= {} attrs_with_defaults = self.class.attributes.each_with_object({}) do |(attribute, ), res| if attrs.key?(attribute) res[attribute] = attrs[attribute] elsif .key?(:default) res[attribute] = evaluate_default_value([:default]) end end attrs_virtual = attrs.slice(*(attrs.keys - self.class.attributes.keys)) load(attrs_with_defaults.merge(attrs_virtual)) if block block.call(self) end end end |
#range_value ⇒ Object
Return a model’s range key value.
Returns nil
if a range key isn’t declared for a model.
268 269 270 271 272 |
# File 'lib/dynamoid/document.rb', line 268 def range_value if self.class.range_key self[self.class.range_key.to_sym] end end |
#range_value=(value) ⇒ Object
Assign a model’s range key value.
275 276 277 278 279 |
# File 'lib/dynamoid/document.rb', line 275 def range_value=(value) if self.class.range_key self[self.class.range_key.to_sym] = value end end |