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

Instance Attribute Summary

Attributes included from Persistence

#new_record

Attributes included from Fields

#attributes

Instance Method Summary (collapse)

Methods included from Dirty

#clear_changes, #save, #update!, #write_attribute

Methods included from IdentityMap

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

Methods included from Validations

#save, #save!, #valid?

Methods included from Persistence

#delete, #destroy, #dump, #persisted?, #save, #touch, #update, #update!

Methods included from Indexes

#delete_indexes, #save_indexes

Methods included from Fields

#read_attribute, #update_attribute, #update_attributes, #write_attribute

Instance Method Details

- (Object) ==(other)

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

Since:

  • 0.2.0



139
140
141
142
143
144
145
146
# File 'lib/dynamoid/document.rb', line 139

def ==(other)
  if self.class.identity_map_on?
    super
  else
    return false if other.nil?
    other.is_a?(Dynamoid::Document) && self.hash_key == other.hash_key && self.range_value == other.range_value
  end
end

- (Boolean) eql?(other)

Returns:

  • (Boolean)


148
149
150
# File 'lib/dynamoid/document.rb', line 148

def eql?(other)
  self == other
end

- (Object) hash



152
153
154
# File 'lib/dynamoid/document.rb', line 152

def hash
  hash_key.hash ^ range_value.hash
end

- (Object) hash_key

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

Since:

  • 0.4.0



172
173
174
# File 'lib/dynamoid/document.rb', line 172

def hash_key
  self.send(self.class.hash_key)
end

- (Object) hash_key=(value)

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

Since:

  • 0.4.0



179
180
181
# File 'lib/dynamoid/document.rb', line 179

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

- (Dynamoid::Document) initialize(attrs = {})

Initialize a new object.

Parameters:

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

    Attributes with which to create the object.

Returns:

Since:

  • 0.2.0



122
123
124
125
126
127
128
129
130
# File 'lib/dynamoid/document.rb', line 122

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

    load(attrs)
  end
end

- (Object) load(attrs)



132
133
134
# File 'lib/dynamoid/document.rb', line 132

def load(attrs)
  self.class.undump(attrs).each {|key, value| send "#{key}=", value }
end

- (Object) range_value



183
184
185
186
187
# File 'lib/dynamoid/document.rb', line 183

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

- (Object) range_value=(value)



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

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

- (Dynamoid::Document) reload

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. This is a consistent read.

Returns:

Since:

  • 0.2.0



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

def reload
  range_key_value = range_value ? dumped_range_value : nil
  self.attributes = self.class.find(hash_key, :range_key => range_key_value, :consistent_read => true).attributes
  @associations.values.each(&:reset)
  self
end