Class: DynamoidAdvancedWhere::FilterBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid_advanced_where/filter_builder.rb

Constant Summary collapse

VALID_COMPARETORS_FOR_RANGE_FILTER =
[
  Nodes::GreaterThanNode,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_node:, klass:) ⇒ FilterBuilder

Returns a new instance of FilterBuilder.



13
14
15
16
17
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 13

def initialize(root_node:, klass:)
  node = root_node.child_node
  self.expression_node = node.is_a?(Nodes::AndNode) ? node : Nodes::AndNode.new(node)
  self.klass = klass
end

Instance Attribute Details

#expression_nodeObject

Returns the value of attribute expression_node.



11
12
13
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 11

def expression_node
  @expression_node
end

#klassObject

Returns the value of attribute klass.



11
12
13
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 11

def klass
  @klass
end

#query_filter_nodeObject

Returns the value of attribute query_filter_node.



11
12
13
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 11

def query_filter_node
  @query_filter_node
end

#range_key_nodeObject

Returns the value of attribute range_key_node.



11
12
13
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 11

def range_key_node
  @range_key_node
end

Instance Method Details

#extractable_fields_for_hash_and_rangeObject

Returns a hash of the field name and the node that filters on it



57
58
59
60
61
62
63
64
65
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 57

def extractable_fields_for_hash_and_range
  expression_node.child_nodes.each_with_object({}) do |node, hash|
    next unless node.respond_to?(:lh_operation) &&
                node.lh_operation.is_a?(Nodes::FieldNode) &&
                node.lh_operation.field_path.length == 1

    hash[node.lh_operation.field_path[0].to_s] = node
  end
end

#index_nodesObject



19
20
21
22
23
24
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 19

def index_nodes
  [
    query_filter_node,
    range_key_node,
  ].compact
end

#select_node_for_query_filter(node) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 46

def select_node_for_query_filter(node)
  raise 'node not found in expression' unless expression_node.child_nodes.include?(node)

  self.query_filter_node = node

  self.expression_node = Nodes::AndNode.new(
    *(expression_node.child_nodes - [node])
  )
end

#select_node_for_range_key(node) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 36

def select_node_for_range_key(node)
  raise 'node not found in expression' unless expression_node.child_nodes.include?(node)

  self.range_key_node = node

  self.expression_node = Nodes::AndNode.new(
    *(expression_node.child_nodes - [node])
  )
end

#to_query_filterObject



26
27
28
29
30
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 26

def to_query_filter
  {
    key_condition_expression: key_condition_expression,
  }.merge!(expression_filters)
end

#to_scan_filterObject



32
33
34
# File 'lib/dynamoid_advanced_where/filter_builder.rb', line 32

def to_scan_filter
  expression_filters
end