class Dynamoid::AdapterPlugin::AwsSdkV3::ItemUpdater

Mimics behavior of the yielded object on DynamoDB’s update_item API (high level).

Constants

ADD
DELETE
PUT

Attributes

key[R]
range_key[R]
table[R]

Public Class Methods

new(table, key, range_key = nil) click to toggle source
# File lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb, line 15
def initialize(table, key, range_key = nil)
  @table = table
  @key = key
  @range_key = range_key
  @additions = {}
  @deletions = {}
  @updates   = {}
end

Public Instance Methods

add(values) click to toggle source

Adds the given values to the values already stored in the corresponding columns. The column must contain a Set or a number.

@param [Hash] values keys of the hash are the columns to update, values

are the values to add. values must be a Set, Array, or Numeric
# File lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb, line 31
def add(values)
  @additions.merge!(sanitize_attributes(values))
end
attribute_updates() click to toggle source

Returns an AttributeUpdates hash suitable for passing to the V2 Client API

# File lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb, line 59
def attribute_updates
  result = {}

  @additions.each do |k, v|
    result[k] = {
      action: ADD,
      value: v
    }
  end

  @deletions.each do |k, v|
    result[k] = {
      action: DELETE
    }
    result[k][:value] = v unless v.nil?
  end

  @updates.each do |k, v|
    result[k] = {
      action: PUT,
      value: v
    }
  end

  result
end
delete(values) click to toggle source

Removes values from the sets of the given columns

@param [Hash|Symbol|String] values keys of the hash are the columns, values are Arrays/Sets of items

to remove
# File lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb, line 41
def delete(values)
  if values.is_a?(Hash)
    @deletions.merge!(sanitize_attributes(values))
  else
    @deletions.merge!(values.to_s => nil)
  end
end
set(values) click to toggle source

Replaces the values of one or more attributes

# File lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb, line 52
def set(values)
  @updates.merge!(sanitize_attributes(values))
end

Private Instance Methods

sanitize_attributes(attributes) click to toggle source
# File lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb, line 88
def sanitize_attributes(attributes)
  attributes.transform_values do |v|
    if v.is_a?(Hash)
      v.stringify_keys
    elsif v.is_a?(Set) && v.empty?
      nil
    elsif v.is_a?(String) && v.empty?
      nil
    else
      v
    end
  end
end