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 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.
- #inspect ⇒ 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, #changes_applied, #clear_attribute_changes, #clear_changes_information, #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.
237 238 239 240 241 242 243 244 245 |
# File 'lib/dynamoid/document.rb', line 237 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.
252 253 254 |
# File 'lib/dynamoid/document.rb', line 252 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.
262 263 264 |
# File 'lib/dynamoid/document.rb', line 262 def hash [hash_key, range_value].hash end |
#hash_key ⇒ Object
Return a model’s hash key value.
269 270 271 |
# File 'lib/dynamoid/document.rb', line 269 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.
277 278 279 |
# File 'lib/dynamoid/document.rb', line 277 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
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/dynamoid/document.rb', line 205 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 yield(self) end end end |
#inspect ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/dynamoid/document.rb', line 297 def inspect # attributes order is: # - partition key # - sort key # - user defined attributes # - timestamps - created_at/updated_at names = [self.class.hash_key] names << self.class.range_key if self.class.range_key names += self.class.attributes.keys - names - %i[created_at updated_at] names << :created_at if self.class.attributes.key?(:created_at) names << :updated_at if self.class.attributes.key?(:updated_at) inspection = names.map do |name| value = read_attribute(name) "#{name}: #{value.inspect}" end.join(', ') "#<#{self.class.name} #{inspection}>" end |
#range_value ⇒ Object
Return a model’s range key value.
Returns nil
if a range key isn’t declared for a model.
284 285 286 287 288 |
# File 'lib/dynamoid/document.rb', line 284 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.
291 292 293 294 295 |
# File 'lib/dynamoid/document.rb', line 291 def range_value=(value) if self.class.range_key self[self.class.range_key.to_sym] = value end end |