Module: Dynamoid::Document
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/dynamoid/document.rb
Overview
This is the base module for all domain objects that need to be persisted to the database as documents.
Single Table Inheritance (STI)
Dynamoid supports Single Table Inheritance across class hierarchies stored
in the same DynamoDB table. Subclasses share the base table, inherit fields,
and automatically populate an STI type attribute when the type field
(or custom inheritance_field) is declared on the base class:
class Animal
include Dynamoid::Document
field :name, :string
field :type, :string
end
class Dog < Animal
field :bark_volume, :integer
end
class Cat < Animal
field :lives, :integer, default: 9
end
See ClassMethods#inheritance_field, ClassMethods#sti_name, ClassMethods#sti_class_for, and ClassMethods#abstract_class for customization hooks.
Defined Under Namespace
Modules: ClassMethods
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
Methods included from Validations
#update_attribute, #update_attribute!, #valid?
Methods included from Fields
#attributes_before_type_cast, #read_attribute, #read_attribute_before_type_cast, #write_attribute
Methods included from Dirty
#changed, #changed?, #changed_attributes, #changes, #changes_applied, #clear_attribute_changes, #clear_changes_information, #previous_changes, #restore_attributes
Methods included from Loadable
Methods included from Persistence
#decrement, #decrement!, #delete, #destroy, #destroy!, #increment, #increment!, #persisted?, #save, #save!, #touch, #update, #update!, #update_attribute, #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.
309 310 311 312 313 314 315 316 317 |
# File 'lib/dynamoid/document.rb', line 309 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.
324 325 326 |
# File 'lib/dynamoid/document.rb', line 324 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.
334 335 336 |
# File 'lib/dynamoid/document.rb', line 334 def hash [hash_key, range_value].hash end |
#hash_key ⇒ Object
Return a model's hash key value.
341 342 343 |
# File 'lib/dynamoid/document.rb', line 341 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.
349 350 351 |
# File 'lib/dynamoid/document.rb', line 349 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
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/dynamoid/document.rb', line 277 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
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/dynamoid/document.rb', line 369 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.
356 357 358 359 360 |
# File 'lib/dynamoid/document.rb', line 356 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.
363 364 365 366 367 |
# File 'lib/dynamoid/document.rb', line 363 def range_value=(value) if self.class.range_key self[self.class.range_key.to_sym] = value end end |