class AWS::DynamoDB::AttributeCollection::UpdateBuilder

Used to build a batch of updates to an item’s attributes. See {AttributeCollection#update} for more information.

Attributes

updates[R]

@api private

Public Class Methods

new() click to toggle source

@api private

# File lib/aws/dynamo_db/attribute_collection.rb, line 237
def initialize
  @updates = {}
end

Public Instance Methods

add(attributes) click to toggle source

Adds to the values of one or more attributes. See {AttributeCollection#add} for more information.

# File lib/aws/dynamo_db/attribute_collection.rb, line 261
def add attributes
  attribute_updates("ADD", attributes)
end
delete(*args) click to toggle source

Deletes one or more attributes or attribute values. See {AttributeCollection#delete} for more information.

# File lib/aws/dynamo_db/attribute_collection.rb, line 268
def delete *args
  if args.first.kind_of?(Hash)
    attribute_updates("DELETE",
                      args.shift,
                      :setify => true)
  else
    add_updates(args.inject({}) do |u, name|
                  u.update(name.to_s => {
                             :action => "DELETE"
                           })
                end)
  end
end
merge!(attributes)
Alias for: set
put(attributes)
Alias for: set
set(attributes) click to toggle source

Replaces the values of one or more attributes. See {AttributeCollection#set} for more information.

# File lib/aws/dynamo_db/attribute_collection.rb, line 243
def set attributes
  to_delete = []
  attributes = attributes.inject({}) do |attributes, (name, value)|
    if value == nil
      to_delete << name
    else
      attributes[name] = value
    end
    attributes
  end
  attribute_updates("PUT", attributes)
  delete(*to_delete)
end
Also aliased as: put, merge!

Private Instance Methods

add_updates(new_updates) click to toggle source
# File lib/aws/dynamo_db/attribute_collection.rb, line 298
def add_updates(new_updates)
  updates.merge!(new_updates) do |name, old, new|
    raise ArgumentError, "conflicting updates for attribute #{name}"
  end
end
attribute_updates(action, attributes, our_opts = {}) click to toggle source
# File lib/aws/dynamo_db/attribute_collection.rb, line 283
def attribute_updates(action, attributes, our_opts = {})
  new_updates = attributes.inject({}) do |new_updates, (name, value)|
    name = name.to_s
    context = "in value for attribute #{name}"
    value = [value].flatten if our_opts[:setify]
    new_updates.update(name => {
                         :action => action,
                         :value =>
                         format_attribute_value(value, context)
                       })
  end
  add_updates(new_updates)
end