Class: Dynamoid::AdapterPlugin::AwsSdkV2::ItemUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid/adapter_plugin/aws_sdk_v2.rb

Overview

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

Constant Summary collapse

ADD =
"ADD".freeze
DELETE =
"DELETE".freeze
PUT =
"PUT".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, key, range_key = nil) ⇒ ItemUpdater

Returns a new instance of ItemUpdater.



944
945
946
947
948
949
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 944

def initialize(table, key, range_key = nil)
  @table = table; @key = key, @range_key = range_key
  @additions = {}
  @deletions = {}
  @updates   = {}
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



942
943
944
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 942

def key
  @key
end

#range_keyObject (readonly)

Returns the value of attribute range_key.



942
943
944
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 942

def range_key
  @range_key
end

#tableObject (readonly)

Returns the value of attribute table.



942
943
944
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 942

def table
  @table
end

Instance Method Details

#add(values) ⇒ Object

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

Parameters:

  • vals (Hash)

    keys of the hash are the columns to update, vals are the values to add. values must be a Set, Array, or Numeric



958
959
960
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 958

def add(values)
  @additions.merge!(values)
end

#delete(values) ⇒ Object

Removes values from the sets of the given columns

Parameters:

  • values (Hash)

    keys of the hash are the columns, values are Arrays/Sets of items to remove



968
969
970
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 968

def delete(values)
  @deletions.merge!(values)
end

#set(values) ⇒ Object

Replaces the values of one or more attributes



975
976
977
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 975

def set(values)
  @updates.merge!(values)
end

#to_hObject

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



982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v2.rb', line 982

def to_h
  ret = {}

  @additions.each do |k,v|
    ret[k.to_s] = {
      action: ADD,
      value: v
    }
  end
  @deletions.each do |k,v|
    ret[k.to_s] = {
      action: DELETE,
      value: v
    }
  end
  @updates.each do |k,v|
    ret[k.to_s] = {
      action: PUT,
      value: v
    }
  end

  ret
end