Sha256: ffbf758f145db0fc6b8f5f1bd2339b12f7e118322467d495dc0750dbc57e5681

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 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|
        if (value = attribute.to_store(read_attribute(attribute.name)))
          attributes_with_values[attribute.persisted_name] = value
        end
      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.0.10 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.9 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.8 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.7 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.6 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.5 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.4 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.3 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.2 lib/toy/dynamo/persistence.rb
toy-dynamo-0.0.1 lib/toy/dynamo/persistence.rb