Module: Dynamoid::Document

Extended by:
ActiveSupport::Concern
Includes:
Components
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.

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from Finders

Finders::RANGE_MAP

Constants included from Persistence

Persistence::UNIX_EPOCH_DATE

Constants included from Fields

Fields::PERMITTED_KEY_TYPES

Instance Attribute Summary

Attributes included from Persistence

#new_record

Attributes included from Fields

#attributes

Instance Method Summary collapse

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

#save, #save!, #valid?

Methods included from Persistence

#decrement, #decrement!, #delete, #destroy, #destroy!, #increment, #increment!, #persisted?, #save, #touch, #update, #update!, #update_attribute, #update_attributes, #update_attributes!

Methods included from Fields

#attributes_before_type_cast, #read_attribute, #read_attribute_before_type_cast, #write_attribute

Instance Method Details

#==(other) ⇒ Object

An object is equal to another object if their ids are equal.

Since:

  • 0.2.0



346
347
348
349
350
351
352
353
# File 'lib/dynamoid/document.rb', line 346

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) ⇒ Boolean

Returns:

  • (Boolean)


355
356
357
# File 'lib/dynamoid/document.rb', line 355

def eql?(other)
  self == other
end

#hashObject



359
360
361
# File 'lib/dynamoid/document.rb', line 359

def hash
  hash_key.hash ^ range_value.hash
end

#hash_keyObject

Return an object’s hash key, regardless of what it might be called to the object.

Since:

  • 0.4.0



384
385
386
# File 'lib/dynamoid/document.rb', line 384

def hash_key
  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.

Since:

  • 0.4.0



391
392
393
# File 'lib/dynamoid/document.rb', line 391

def hash_key=(value)
  send("#{self.class.hash_key}=", value)
end

#initialize(attrs = {}) ⇒ Dynamoid::Document

Initialize a new object.

Parameters:

  • attrs (Hash) (defaults to: {})

    Attributes with which to create the object.

Returns:

Since:

  • 0.2.0



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/dynamoid/document.rb', line 305

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 ||= {}
    @attributes_before_type_cast ||= {}

    self.class.attributes.each do |_, options|
      if options[:type].is_a?(Class) && options[:default]
        raise 'Dynamoid class-type fields do not support default values'
      end
    end

    attrs_with_defaults = {}
    self.class.attributes.each do |attribute, options|
      attrs_with_defaults[attribute] = if attrs.key?(attribute)
                                         attrs[attribute]
                                       elsif options.key?(:default)
                                         evaluate_default_value(options[:default])
                                       end
    end

    attrs_virtual = attrs.slice(*(attrs.keys - self.class.attributes.keys))

    load(attrs_with_defaults.merge(attrs_virtual))
  end
end

#load(attrs) ⇒ Object



337
338
339
340
341
# File 'lib/dynamoid/document.rb', line 337

def load(attrs)
  attrs.each do |key, value|
    send("#{key}=", value) if respond_to?("#{key}=")
  end
end

#range_valueObject



395
396
397
398
399
# File 'lib/dynamoid/document.rb', line 395

def range_value
  if range_key = self.class.range_key
    send(range_key)
  end
end

#range_value=(value) ⇒ Object



401
402
403
# File 'lib/dynamoid/document.rb', line 401

def range_value=(value)
  send("#{self.class.range_key}=", value)
end

#reloadDynamoid::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.

Returns:

Since:

  • 0.2.0



369
370
371
372
373
374
375
376
377
378
379
# File 'lib/dynamoid/document.rb', line 369

def reload
  options = { consistent_read: true }

  if self.class.range_key
    options[:range_key] = range_value
  end

  self.attributes = self.class.find(hash_key, options).attributes
  @associations.values.each(&:reset)
  self
end