Class: Dynomite::Item::Query::Params::ExpressionAttribute

Inherits:
Base
  • Object
show all
Defined in:
lib/dynomite/item/query/params/expression_attribute.rb

Constant Summary

Constants included from Types

Types::TYPE_MAP

Instance Method Summary collapse

Methods inherited from Base

#expression_string, #function_names, #join_expressions

Methods included from Helpers

#all_where_field_names, #all_where_fields, #disable_index_for_any_or?, #disable_index_for_consistent_read?, #disable_index_for_not?, #normalize_expression_path, #normalize_project_expression, #query, #scan_required?, #with_where_groups

Methods included from Types

#type_map

Constructor Details

#initialize(relation) ⇒ ExpressionAttribute

Returns a new instance of ExpressionAttribute.



3
4
5
6
# File 'lib/dynomite/item/query/params/expression_attribute.rb', line 3

def initialize(relation)
  @relation = relation
  @names, @values = {}, {}
end

Instance Method Details

#buildObject



18
19
20
21
22
# File 'lib/dynomite/item/query/params/expression_attribute.rb', line 18

def build
  build_where
  build_functions
  build_project_expression
end

#build_functionsObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dynomite/item/query/params/expression_attribute.rb', line 50

def build_functions
  # Essentially
  #    @names.merge!(Function::AttributeExists.new(@query).attribute_names)
  #    @names.merge!(Function::AttributeType.new(@query).attribute_names)
  #    @names.merge!(Function::BeginsWith.new(@query).attribute_names)
  function_names.each do |function_name|
    function = Function.const_get(function_name.camelize).new(@relation.query)
    @names.merge!(function.attribute_names)
    @values.merge!(function.attribute_values)
  end
end

#build_project_expressionObject



62
63
64
65
66
67
68
69
# File 'lib/dynomite/item/query/params/expression_attribute.rb', line 62

def build_project_expression
  return if @relation.query[:projection_expression].nil?
  projection_expression = normalize_project_expression(@relation.query[:projection_expression])
  projection_expression.each do |field|
    field = field.to_s
    @names["##{field}"] = field
  end
end

#build_whereObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dynomite/item/query/params/expression_attribute.rb', line 25

def build_where
  with_where_groups do |where_group|
    where_group.each do |where_field|
      field = where_field.field
      reference = where_field.reference
      value = where_field.value
      @names["##{reference}"] = field

      model_class = @relation.source
      meta = model_class.fields_meta[field.to_sym] # can be nil if field is not defined
      type = meta ? meta[:type] : :infer

      if where_field.operator == "in" || value.is_a?(Array)
        array = Array(value)
        array.each_with_index do |v, i|
          @values[":#{reference}_#{i}"] = cast_to_type(type, v, on: :query)
        end
      else
        @values[":#{reference}"] = cast_to_type(type, value, on: :query)
      end
    end
  end
end

#namesObject



8
9
10
11
# File 'lib/dynomite/item/query/params/expression_attribute.rb', line 8

def names
  build
  @names
end

#valuesObject



13
14
15
16
# File 'lib/dynomite/item/query/params/expression_attribute.rb', line 13

def values
  build
  @values
end