Module: Toy::Persistence

Defined in:
lib/toy/dynamo/persistence.rb

Instance Method Summary collapse

Instance Method Details

#deleteObject



28
29
30
31
32
33
34
35
# File 'lib/toy/dynamo/persistence.rb', line 28

def delete
  @_destroyed = true
  options = {}
  if self.class.dynamo_table.range_keys && primary_range_key = self.class.dynamo_table.range_keys.find{|k| k[:primary_range_key]}
    options[:range_value] = read_attribute(primary_range_key[:attribute_name])
  end
  adapter.delete(persisted_id, options)
end

#persistObject



24
25
26
# File 'lib/toy/dynamo/persistence.rb', line 24

def persist
  adapter.write(persisted_id, persisted_attributes, {:update_item => !self.new_record?})
end

#persisted_attributesObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/toy/dynamo/persistence.rb', line 4

def persisted_attributes
  attributes_with_values = {}
  attributes_to_persist = []

  if self.new_record?
    attributes_to_persist = self.class.persisted_attributes
  else
    attributes_to_persist = self.class.persisted_attributes.select { |a|
      # Persist changed attributes and always the range key if applicable (for lookup)
      self.changed_attributes.keys.include?(a.name) || (self.class.dynamo_table.range_keys && (primary_range_key = self.class.dynamo_table.range_keys.find{|k| k[:primary_range_key]}) && primary_range_key[:attribute_name] == a.name)
    }
  end

  attributes_to_persist.each do |attribute|
    attributes_with_values[attribute.persisted_name] = attribute.to_store(read_attribute(attribute.name))
  end

  attributes_with_values
end