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 Fields

Fields::PERMITTED_KEY_TYPES

Constants included from Persistence

Persistence::UNIX_EPOCH_DATE

Instance Attribute Summary

Attributes included from Fields

#attributes

Attributes included from Persistence

#new_record

Instance Method Summary collapse

Methods included from IdentityMap

clear, #delete, #identity_map, #identity_map_key, #save

Methods included from Validations

#save, #save!, #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, #previous_changes, #reload, #restore_attributes, #save, #save!, #update, #update!

Methods included from Loadable

#load, #reload

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

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

Since:

  • 0.2.0



160
161
162
163
164
165
166
167
168
# File 'lib/dynamoid/document.rb', line 160

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)


170
171
172
# File 'lib/dynamoid/document.rb', line 170

def eql?(other)
  self == other
end

#hashObject



174
175
176
# File 'lib/dynamoid/document.rb', line 174

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



181
182
183
# File 'lib/dynamoid/document.rb', line 181

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



188
189
190
# File 'lib/dynamoid/document.rb', line 188

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dynamoid/document.rb', line 134

def initialize(attrs = {})
  run_callbacks :initialize do
    @new_record = true
    @attributes ||= {}
    @associations ||= {}
    @attributes_before_type_cast ||= {}

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

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

    load(attrs_with_defaults.merge(attrs_virtual))
  end
end

#range_valueObject



192
193
194
195
196
# File 'lib/dynamoid/document.rb', line 192

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

#range_value=(value) ⇒ Object



198
199
200
# File 'lib/dynamoid/document.rb', line 198

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