Class: Dynamoid::AdapterPlugin::AwsSdkV3::ItemUpdater

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

Overview

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

Constant Summary collapse

ADD =
'ADD'
DELETE =
'DELETE'
PUT =
'PUT'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ItemUpdater.



15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



13
14
15
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb', line 13

def key
  @key
end

#range_keyObject (readonly)

Returns the value of attribute range_key.



13
14
15
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb', line 13

def range_key
  @range_key
end

#tableObject (readonly)

Returns the value of attribute table.



13
14
15
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb', line 13

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:

  • values (Hash)

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



31
32
33
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb', line 31

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

#attribute_updatesObject

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb', line 71

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) ⇒ Object

Removes values from the sets of the given columns

Parameters:

  • values (Hash|Symbol|String)

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



41
42
43
44
45
46
47
# 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) ⇒ Object

Replaces the values of one or more attributes



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb', line 52

def set(values)
  values_sanitized = sanitize_attributes(values)

  if Dynamoid.config.store_attribute_with_nil_value
    @updates.merge!(values_sanitized)
  else
    # delete explicitly attributes if assigned nil value and configured
    # to not store nil values
    values_to_update = values_sanitized.reject { |_, v| v.nil? }
    values_to_delete = values_sanitized.select { |_, v| v.nil? }

    @updates.merge!(values_to_update)
    @deletions.merge!(values_to_delete)
  end
end