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, #priority, #render_tree

Methods inherited from AbstractMatcher

#cached_proc, #debug_proc, define_matcher, #initialize, #match, #match?, #priority, #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:



55
56
57
58
59
# File 'lib/mongory/matchers/hash_condition_matcher.rb', line 55

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

  raise TypeError, 'condition needs a Hash.'
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:



45
46
47
48
49
50
51
52
53
# File 'lib/mongory/matchers/hash_condition_matcher.rb', line 45

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.sort_by(&:priority)
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
# File 'lib/mongory/matchers/hash_condition_matcher.rb', line 34

def raw_proc
  combine_procs_with_and(*matchers.map(&:to_proc))
end