Class: Commutator::Expressions::ConditionExpression

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

Overview

See: goo.gl/bhIUgS

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

Note: where, and, or, and_not, or_not, where_not pass their arguments to the Statement constructor. Read the docs there for placeholder syntax.

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

.where('token_channel = :?', values: ['123'])
.and('ts > :?', values: [1344])

exp.or do |expression|

expression
  .where('attribute_exists(#?)', names: ['count'])
  .and('attribute_exists(something)')

end

Later…

exp.and { |e| e.where(‘ts BETWEEN :? AND :?’, values: [1, 2]) }

The expression above would produce the following with ‘to_s`

“token_channel = :VALUE_757d204b68e8e1c419288694ab908f55 AND

ts > :VALUE_a50abba8132a77191791390c3eb19fe7 OR
(attribute_exists(#NAME_d61a1061060c9e9691027c42d6766b90) AND attribute_exists(something)) AND 
(ts BETWEEN :VALUE_c4ca4238a0b923820dcc509a6f75849b AND :VALUE_c81e728d9d4c2f636f067f89cc14862c)"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ConditionExpression.



36
37
38
39
# File 'lib/commutator/expressions/condition_expression.rb', line 36

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.



34
35
36
# File 'lib/commutator/expressions/condition_expression.rb', line 34

def attribute_names
  @attribute_names
end

#attribute_valuesObject (readonly)

Returns the value of attribute attribute_values.



34
35
36
# File 'lib/commutator/expressions/condition_expression.rb', line 34

def attribute_values
  @attribute_values
end

Instance Method Details

#and(*args, &block) ⇒ Object Also known as: where



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

def and(*args, &block)
  add_statement('AND', *args, &block)
  self
end

#and_not(*args, &block) ⇒ Object Also known as: where_not



60
61
62
63
# File 'lib/commutator/expressions/condition_expression.rb', line 60

def and_not(*args, &block)
  add_statement(%w(AND NOT), *args, &block)
  self
end

#or(*args, &block) ⇒ Object



45
46
47
48
# File 'lib/commutator/expressions/condition_expression.rb', line 45

def or(*args, &block)
  add_statement('OR', *args, &block)
  self
end

#or_not(*args, &block) ⇒ Object



50
51
52
53
# File 'lib/commutator/expressions/condition_expression.rb', line 50

def or_not(*args, &block)
  add_statement(%w(OR NOT), *args, &block)
  self
end

#statementsObject



41
42
43
# File 'lib/commutator/expressions/condition_expression.rb', line 41

def statements
  @statements ||= []
end

#to_s(wrap: true) ⇒ Object



68
69
70
71
# File 'lib/commutator/expressions/condition_expression.rb', line 68

def to_s(wrap: true)
  str = statements.map(&:to_s).join(' ')
  wrap ? "(#{str})" : str
end