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 Indexes

Indexes::PERMITTED_KEY_DYNAMODB_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!, #update_attribute, #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) ⇒ 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.

Returns:

  • (true|false)

Since:

  • 0.2.0



221
222
223
224
225
226
227
228
229
# File 'lib/dynamoid/document.rb', line 221

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.

Returns:

  • (true|false)


236
237
238
# File 'lib/dynamoid/document.rb', line 236

def eql?(other)
  self == other
end

#hashInteger

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.

Returns:

  • (Integer)


246
247
248
# File 'lib/dynamoid/document.rb', line 246

def hash
  hash_key.hash ^ range_value.hash
end

#hash_keyObject

Return a model’s hash key value.

Since:

  • 0.4.0



253
254
255
# File 'lib/dynamoid/document.rb', line 253

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.

Since:

  • 0.4.0



261
262
263
# File 'lib/dynamoid/document.rb', line 261

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

Parameters:

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

    Attributes with which to create the document

  • block (Proc)

    Block to process a document after initialization

Returns:

Since:

  • 0.2.0



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dynamoid/document.rb', line 189

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
      block.call(self)
    end
  end
end

#range_valueObject

Return a model’s range key value.

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



268
269
270
271
272
# File 'lib/dynamoid/document.rb', line 268

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.



275
276
277
278
279
# File 'lib/dynamoid/document.rb', line 275

def range_value=(value)
  if self.class.range_key
    self[self.class.range_key.to_sym] = value
  end
end