Sha256: 7a496854bb2b50ea7d332000db85b6d0e07ee80edb31987168624fbc201db254

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

module Toy
  module Persistence

    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 && self.class.dynamo_table.range_keys.find{|k| k[: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

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

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

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
toy-dynamo-0.1.4 lib/toy/dynamo/persistence.rb
toy-dynamo-0.1.3 lib/toy/dynamo/persistence.rb
toy-dynamo-0.1.2 lib/toy/dynamo/persistence.rb
toy-dynamo-0.1.1 lib/toy/dynamo/persistence.rb
toy-dynamo-0.1.0 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.15 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.14 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.13 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.12 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.11 lib/toy/dynamo/persistence.rb