Module: Dynamoid::Document

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

Instance Attribute Summary

Attributes included from Fields

#attributes

Attributes included from Persistence

#new_record

Instance Method Summary collapse

Methods included from IdentityMap

clear, #identity_map

Methods included from Validations

#valid?

Methods included from Fields

#attributes_before_type_cast, #read_attribute, #read_attribute_before_type_cast, #write_attribute

Methods included from Dirty

#changed, #changed?, #changed_attributes, #changes, #previous_changes, #restore_attributes

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


227
228
229
230
231
232
233
234
235
# File 'lib/dynamoid/document.rb', line 227

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)

242
243
244
# File 'lib/dynamoid/document.rb', line 242

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)

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

def hash
  hash_key.hash ^ range_value.hash
end

#hash_keyObject

Return a model's hash key value.

Since:

  • 0.4.0


259
260
261
# File 'lib/dynamoid/document.rb', line 259

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


267
268
269
# File 'lib/dynamoid/document.rb', line 267

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


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/dynamoid/document.rb', line 195

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.


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

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.


281
282
283
284
285
# File 'lib/dynamoid/document.rb', line 281

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