Class: Mongory::Matchers::HashConditionMatcher

Inherits:
AbstractMultiMatcher show all
Defined in:
lib/mongory/matchers/hash_condition_matcher.rb

Overview

HashConditionMatcher is responsible for handling field-level query conditions.

It receives a Hash of key-value pairs and delegates each one to an appropriate matcher based on whether the key is a recognized operator or a data field path.

Each subcondition is matched independently using the :all? strategy, meaning all subconditions must match for the entire HashConditionMatcher to succeed. For empty conditions, it returns true (using TRUE_PROC).

This matcher plays a central role in dispatching symbolic query conditions to the appropriate field or operator matcher.

Examples:

Basic field matching

matcher = HashConditionMatcher.build({ age: { :$gt => 30 }, active: true })
matcher.match?(record) #=> true only if all subconditions match

Empty conditions

matcher = HashConditionMatcher.build({})
matcher.match?(record) #=> true (uses TRUE_PROC)

See Also:

Direct Known Subclasses

ElemMatchMatcher, EveryMatcher, QueryMatcher

Constant Summary

Constants inherited from AbstractMultiMatcher

AbstractMultiMatcher::FALSE_PROC, AbstractMultiMatcher::TRUE_PROC

Constants inherited from AbstractMatcher

AbstractMatcher::KEY_NOT_FOUND

Instance Attribute Summary

Attributes inherited from AbstractMatcher

#condition, #context

Instance Method Summary collapse

Methods inherited from AbstractMultiMatcher

build_or_unwrap, #render_tree

Methods inherited from AbstractMatcher

#cached_proc, #debug_proc, define_matcher, #initialize, #match, #match?, #render_tree, #uniq_key

Methods included from Utils

included, included_classes, #is_blank?, #is_present?

Constructor Details

This class inherits a constructor from Mongory::Matchers::AbstractMatcher

Instance Method Details

#check_validity!Object

Raises:



76
77
78
79
80
# File 'lib/mongory/matchers/hash_condition_matcher.rb', line 76

def check_validity!
  return super if @condition.is_a?(Hash)

  raise TypeError, 'condition needs a Hash.'
end

#combine_procs(left, *rest) ⇒ Proc

Recursively combines multiple matcher procs with AND logic. This method optimizes the combination of multiple matchers by building a balanced tree of AND operations.

Examples:

combine_procs(proc1, proc2, proc3)
#=> proc { |record| proc1.call(record) && proc2.call(record) && proc3.call(record) }

Parameters:

  • left (Proc)

    The left matcher proc to combine

  • rest (Array<Proc>)

    The remaining matcher procs to combine

Returns:

  • (Proc)

    A new proc that combines all matchers with AND logic



50
51
52
53
54
55
56
57
# File 'lib/mongory/matchers/hash_condition_matcher.rb', line 50

def combine_procs(left, *rest)
  return left if rest.empty?

  right = combine_procs(*rest)
  Proc.new do |record|
    left.call(record) && right.call(record)
  end
end

#matchersArray<AbstractMatcher>

Returns the list of matchers for each key-value pair in the condition.

For each pair:

  • If the key is a registered operator, uses the corresponding matcher
  • Otherwise, wraps the value in a FieldMatcher for field path matching

Returns:



66
67
68
69
70
71
72
73
74
# File 'lib/mongory/matchers/hash_condition_matcher.rb', line 66

define_instance_cache_method(:matchers) do
  @condition.map do |key, value|
    if (matcher_class = Matchers.lookup(key))
      matcher_class.build(value, context: @context)
    else
      FieldMatcher.build(key, value, context: @context)
    end
  end
end

#raw_procProc

Creates a raw Proc that performs the hash condition matching operation. The Proc combines all submatcher Procs and returns true only if all match. For empty conditions, returns TRUE_PROC.

Returns:

  • (Proc)

    a Proc that performs the hash condition matching operation



34
35
36
37
38
# File 'lib/mongory/matchers/hash_condition_matcher.rb', line 34

def raw_proc
  return TRUE_PROC if matchers.empty?

  combine_procs(*matchers.map(&:to_proc))
end