Class: Mongory::Matchers::OrMatcher

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

Overview

OrMatcher implements the $or logical operator.

It evaluates an array of subconditions and returns true if any one of them matches. For empty conditions, it returns false (using FALSE_PROC).

Each subcondition is handled by a HashConditionMatcher with conversion disabled, since the parent matcher already manages data conversion.

This matcher inherits submatcher dispatch and evaluation logic from AbstractMultiMatcher.

Examples:

matcher = OrMatcher.build([
  { age: { :$lt => 18 } },
  { admin: true }
])
matcher.match?(record) #=> true if either condition matches

Empty conditions

matcher = OrMatcher.build([])
matcher.match?(record) #=> false (uses FALSE_PROC)

See Also:

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!void

This method returns an undefined value.

Ensures the condition is an array of hashes.

Raises:



76
77
78
79
80
81
82
83
84
# File 'lib/mongory/matchers/or_matcher.rb', line 76

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

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

  super
end

#combine_procs(left, *rest) ⇒ Proc

Recursively combines multiple matcher procs with OR logic. This method optimizes the combination of multiple matchers by building a balanced tree of OR 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 OR logic



53
54
55
56
57
58
59
60
# File 'lib/mongory/matchers/or_matcher.rb', line 53

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>

Builds an array of matchers from the subconditions. Each subcondition is wrapped in a HashConditionMatcher.

Returns:



66
67
68
69
70
# File 'lib/mongory/matchers/or_matcher.rb', line 66

define_instance_cache_method(:matchers) do
  @condition.map do |condition|
    HashConditionMatcher.build(condition, context: @context)
  end
end

#raw_procProc

Creates a raw Proc that performs the or-matching operation. The Proc combines all submatcher Procs and returns true if any match. For empty conditions, returns FALSE_PROC.

Returns:

  • (Proc)

    a Proc that performs the or-matching operation



37
38
39
40
41
# File 'lib/mongory/matchers/or_matcher.rb', line 37

def raw_proc
  return FALSE_PROC if matchers.empty?

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