module Dynamoid::Document

This is the base module for all domain objects that need to be persisted to the database as documents.

Public Class Methods

new(attrs = {}) click to toggle source

Initialize a new object.

@param [Hash] attrs Attributes with which to create the object.

@return [Dynamoid::Document] the new document

@since 0.2.0

# 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

Public Instance Methods

==(other) click to toggle source

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

@since 0.2.0

Calls superclass method
# 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() click to toggle source

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

@since 0.4.0

# File lib/dynamoid/document.rb, line 153
def hash_key
  self.send(self.class.hash_key)
end
hash_key=(value) click to toggle source

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

@since 0.4.0

# File lib/dynamoid/document.rb, line 160
def hash_key=(value)
  self.send("#{self.class.hash_key}=", value)
end
load(attrs) click to toggle source
# File lib/dynamoid/document.rb, line 122
def load(attrs)
  self.class.undump(attrs).each {|key, value| send "#{key}=", value }
end
range_value() click to toggle source
# 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) click to toggle source
# File lib/dynamoid/document.rb, line 170
def range_value=(value)
  self.send("#{self.class.range_key}=", value)
end
reload() click to toggle source

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.

@return [Dynamoid::Document] the document this method was called on

@since 0.2.0

# 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