Class: Mongory::Matchers::OrMatcher
- Inherits:
-
AbstractMultiMatcher
- Object
- AbstractMatcher
- AbstractMultiMatcher
- Mongory::Matchers::OrMatcher
- 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.
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
Instance Method Summary collapse
-
#check_validity! ⇒ void
Ensures the condition is an array of hashes.
-
#combine_procs(left, *rest) ⇒ Proc
Recursively combines multiple matcher procs with OR logic.
-
#matchers ⇒ Array<AbstractMatcher>
Builds an array of matchers from the subconditions.
-
#raw_proc ⇒ Proc
Creates a raw Proc that performs the or-matching operation.
Methods inherited from AbstractMultiMatcher
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.
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.
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 |
#matchers ⇒ Array<AbstractMatcher>
Builds an array of matchers from the subconditions. Each subcondition is wrapped in a HashConditionMatcher.
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_proc ⇒ Proc
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.
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 |