Class: Commutator::Expressions::UpdateExpression
- Inherits:
-
Object
- Object
- Commutator::Expressions::UpdateExpression
- 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
-
#attribute_names ⇒ Object
readonly
Returns the value of attribute attribute_names.
-
#attribute_values ⇒ Object
readonly
Returns the value of attribute attribute_values.
Instance Method Summary collapse
- #add_to_section(name, exp, names: [], values: []) ⇒ Object
-
#initialize(attribute_names: nil, attribute_values: nil, &block) ⇒ UpdateExpression
constructor
A new instance of UpdateExpression.
- #sections ⇒ Object
- #to_s ⇒ Object
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_names ⇒ Object (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_values ⇒ Object (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 |
#sections ⇒ Object
35 36 37 |
# File 'lib/commutator/expressions/update_expression.rb', line 35 def sections @sections ||= Hash.new { |h, v| h[v] = [] } end |
#to_s ⇒ Object
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 |