Class: DynamoidAdvancedWhere::Nodes::FieldNode

Inherits:
BaseNode
  • Object
show all
Includes:
Concerns::SupportsEquality, Concerns::SupportsExistance
Defined in:
lib/dynamoid_advanced_where/nodes/field_node.rb

Instance Attribute Summary collapse

Attributes inherited from BaseNode

#expression_prefix

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::SupportsExistance

#exists?

Methods included from Concerns::SupportsEquality

#eq, #not_eq

Constructor Details

#initialize(field_path:) ⇒ FieldNode

Returns a new instance of FieldNode.



31
32
33
34
35
# File 'lib/dynamoid_advanced_where/nodes/field_node.rb', line 31

def initialize(field_path:)
  self.field_path = field_path.is_a?(Array) ? field_path : [field_path]
  self.attr_prefix = SecureRandom.hex
  freeze
end

Instance Attribute Details

#attr_prefixObject

Returns the value of attribute attr_prefix.



15
16
17
# File 'lib/dynamoid_advanced_where/nodes/field_node.rb', line 15

def attr_prefix
  @attr_prefix
end

#field_pathObject

Returns the value of attribute field_path.



15
16
17
# File 'lib/dynamoid_advanced_where/nodes/field_node.rb', line 15

def field_path
  @field_path
end

Class Method Details

.create_node(field_path:, attr_config:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dynamoid_advanced_where/nodes/field_node.rb', line 18

def create_node(field_path:, attr_config:)
  specific_klass = FIELD_MAPPING.detect do |config, _type|
    config.respond_to?(:call) ? config.call(attr_config) : config <= attr_config
  end&.last

  unless specific_klass
    raise ArgumentError, "unable to find field type for `#{attr_config}`"
  end

  specific_klass.new(field_path: field_path)
end

Instance Method Details

#expression_attribute_namesObject



50
51
52
53
54
55
56
# File 'lib/dynamoid_advanced_where/nodes/field_node.rb', line 50

def expression_attribute_names
  field_path.each_with_object({}).with_index do |(segment, hsh), i|
    next if segment.is_a?(Integer)

    hsh["##{attr_prefix}#{i}"] = segment
  end
end

#expression_attribute_valuesObject



58
59
60
# File 'lib/dynamoid_advanced_where/nodes/field_node.rb', line 58

def expression_attribute_values
  {}
end

#to_expressionObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dynamoid_advanced_where/nodes/field_node.rb', line 37

def to_expression
  String.new.tap do |s|
    field_path.collect.with_index do |segment, i|
      if segment.is_a?(Integer)
        s << "[#{segment}]"
      else
        s << '.' unless s.blank?
        s << "##{attr_prefix}#{i}"
      end
    end
  end
end