Class: Mongory::Matchers::AndMatcher

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

Overview

AndMatcher implements the $and logical operator.

It evaluates an array of subconditions and returns true only if all of them match. For empty conditions, it returns true (using TRUE_PROC), following MongoDB's behavior.

Unlike other matchers, AndMatcher flattens the underlying matcher tree by delegating each subcondition to a HashConditionMatcher, and further extracting all inner matchers. Duplicate matchers are deduplicated by uniq_key.

This allows the matcher trace (.explain) to render as a flat list of independent conditions.

Examples:

Basic usage

matcher = AndMatcher.build([
  { age: { :$gte => 18 } },
  { name: /foo/ }
])
matcher.match?(record) #=> true if both match

Empty conditions

matcher = AndMatcher.build([])
matcher.match?(record) #=> true (uses TRUE_PROC)

See Also:

Constant Summary

Constants inherited from AbstractMultiMatcher

Mongory::Matchers::AbstractMultiMatcher::FALSE_PROC, Mongory::Matchers::AbstractMultiMatcher::TRUE_PROC

Constants inherited from AbstractMatcher

Mongory::Matchers::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!void

This method returns an undefined value.

Ensures the condition is an array of hashes.

Raises:



59
60
61
62
63
64
65
66
67
# File 'lib/mongory/matchers/and_matcher.rb', line 59

def check_validity!
  raise TypeError, '$and needs an array' unless @condition.is_a?(Array)

  @condition.each do |sub_condition|
    raise TypeError, '$and needs an array of hash' unless sub_condition.is_a?(Hash)
  end

  super
end

#matchersArray<AbstractMatcher>

Returns the flattened list of all matchers from each subcondition.

Each condition is passed to a HashConditionMatcher, then recursively flattened. All matchers are then deduplicated using uniq_key.

Returns:

See Also:



49
50
51
52
53
# File 'lib/mongory/matchers/and_matcher.rb', line 49

define_instance_cache_method(:matchers) do
  @condition.flat_map do |condition|
    HashConditionMatcher.new(condition, context: @context).matchers
  end.uniq(&:uniq_key).sort_by(&:priority)
end

#raw_procProc

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

Returns:

  • (Proc)

    a Proc that performs the AND operation



38
39
40
# File 'lib/mongory/matchers/and_matcher.rb', line 38

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