Class: Commutator::Expressions::UpdateExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/commutator/expressions/update_expression.rb

Overview

See: goo.gl/qiHKO3

Constructs composable UpdateExpression for use in DynamoDB API calls. Call to_s when sending to DynamoDB.

Note: SET, REMOVE, ADD, and DELETE pass their arguments to the Statement constructor. Read the docs there for placeholder syntax.

Usage: exp = Commutator::Expressions::UpdateExpression.new

.set('username = :?', values: ['user1'])
.set('email = :?', values: ['[email protected]'])
.add('login_count :?', values: [1])
.remove('some_attribute')
.delete('some_set :?', values: [10])

The expression above would produce the following with ‘to_s`

“SET username = :VALUE_91120aaf65529fbdc83e66ea160f17bc,

email = :VALUE_73a839aafcf0207ab931d9edd6369a9a
ADD login_count :VALUE_c4ca4238a0b923820dcc509a6f75849b
REMOVE some_attribute
DELETE some_set :VALUE_d3d9446802a44259755d38e6d163e820"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_names: nil, attribute_values: nil, &block) ⇒ UpdateExpression

Returns a new instance of UpdateExpression.



30
31
32
33
# File 'lib/commutator/expressions/update_expression.rb', line 30

def initialize(attribute_names: nil, attribute_values: nil, &block)
  @attribute_names = attribute_names || AttributeNames.new
  @attribute_values = attribute_values || AttributeValues.new
end

Instance Attribute Details

#attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



28
29
30
# File 'lib/commutator/expressions/update_expression.rb', line 28

def attribute_names
  @attribute_names
end

#attribute_valuesObject (readonly)

Returns the value of attribute attribute_values.



28
29
30
# File 'lib/commutator/expressions/update_expression.rb', line 28

def attribute_values
  @attribute_values
end

Instance Method Details

#add_to_section(name, exp, names: [], values: []) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/commutator/expressions/update_expression.rb', line 45

def add_to_section(name, exp, names: [], values: [])
  names.each { |n| attribute_names.add(n) }
  values.each { |v| attribute_values.add(v) }

  sections[name] << Statement.new(exp, names: names, values: values)

  self
end

#sectionsObject



35
36
37
# File 'lib/commutator/expressions/update_expression.rb', line 35

def sections
  @sections ||= Hash.new { |h, v| h[v] = [] }
end

#to_sObject



54
55
56
57
58
# File 'lib/commutator/expressions/update_expression.rb', line 54

def to_s
  sections
    .map { |name, actions| "#{name.upcase} #{actions.join(', ')}" }
    .join(' ')
end