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.



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

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.



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

def attr_prefix
  @attr_prefix
end

#field_pathObject

Returns the value of attribute field_path.



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

def field_path
  @field_path
end

Class Method Details

.create_node(field_path:, attr_config:) ⇒ Object

Raises:

  • (ArgumentError)


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

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

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

  specific_klass.new(field_path: field_path)
end

Instance Method Details

#expression_attribute_namesObject



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

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



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

def expression_attribute_values
  {}
end

#to_expressionObject



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

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