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 = {}) { |self| ... } click to toggle source

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

@param attrs [Hash] Attributes with which to create the document @param block [Proc] Block to process a document after initialization @return [Dynamoid::Document] the new document

@since 0.2.0

# 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, options), res|
      if attrs.key?(attribute)
        res[attribute] = attrs[attribute]
      elsif options.key?(:default)
        res[attribute] = evaluate_default_value(options[: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

Public Instance Methods

==(other) click to toggle source

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.

@return [true|false] @since 0.2.0

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

Check equality of two models.

Works exactly like +==+ does.

@return [true|false]

# File lib/dynamoid/document.rb, line 252
def eql?(other)
  self == other
end
hash() click to toggle source

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.

@return [Integer]

# File lib/dynamoid/document.rb, line 262
def hash
  hash_key.hash ^ range_value.hash
end
hash_key() click to toggle source

Return a model’s hash key value.

@since 0.4.0

# File lib/dynamoid/document.rb, line 269
def hash_key
  self[self.class.hash_key.to_sym]
end
hash_key=(value) click to toggle source

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

@since 0.4.0

# File lib/dynamoid/document.rb, line 277
def hash_key=(value)
  self[self.class.hash_key.to_sym] = value
end
inspect() click to toggle source
# 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() click to toggle source

Return a model’s range key value.

Returns nil if a range key isn’t declared for a model.

# 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) click to toggle source

Assign a model’s range key value.

# 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

Private Instance Methods

dumped_range_value() click to toggle source
# File lib/dynamoid/document.rb, line 319
def dumped_range_value
  Dumping.dump_field(range_value, self.class.attributes[self.class.range_key])
end
evaluate_default_value(val) click to toggle source

Evaluates the default value given, this is used by undump when determining the value of the default given for a field options.

@param val [Object] the attribute’s default value

# File lib/dynamoid/document.rb, line 327
def evaluate_default_value(val)
  if val.respond_to?(:call)
    val.call
  elsif val.duplicable?
    val.dup
  else
    val
  end
end