Class: Mongory::Matchers::ArrayRecordMatcher
- Inherits:
-
AbstractMultiMatcher
- Object
- AbstractMatcher
- AbstractMultiMatcher
- Mongory::Matchers::ArrayRecordMatcher
- Defined in:
- lib/mongory/matchers/array_record_matcher.rb
Overview
ArrayRecordMatcher handles matching against array-type records.
This matcher is used when a field value is an array and needs to be matched
against a condition. It supports both exact array matching and element-wise
comparison through $elemMatch.
For empty conditions, it returns false (using FALSE_PROC).
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
Instance Method Summary collapse
-
#combine_procs(left, *rest) ⇒ Proc
Recursively combines multiple matcher procs with OR logic.
-
#matchers ⇒ Array<AbstractMatcher>
Builds an array of matchers to evaluate the given condition against an array record.
-
#raw_proc ⇒ Proc
Creates a raw Proc that performs the array matching operation.
Methods inherited from AbstractMultiMatcher
build_or_unwrap, #check_validity!, #render_tree
Methods inherited from AbstractMatcher
#cached_proc, #check_validity!, #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
#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.
52 53 54 55 56 57 58 59 |
# File 'lib/mongory/matchers/array_record_matcher.rb', line 52 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 to evaluate the given condition against an array record.
This method returns multiple matchers that will be evaluated using :any? logic:
- An equality matcher for exact array match
- A hash condition matcher if the condition is a hash
- An
$elemMatchmatcher for element-wise comparison
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mongory/matchers/array_record_matcher.rb', line 69 define_instance_cache_method(:matchers) do result = [] result << EqMatcher.build(@condition, context: @context) if @condition.is_a?(Array) result << case @condition when Hash HashConditionMatcher.build(parsed_condition, context: @context) when Regexp ElemMatchMatcher.build({ '$regex' => @condition }, context: @context) else ElemMatchMatcher.build({ '$eq' => @condition }, context: @context) end result end |
#raw_proc ⇒ Proc
Creates a raw Proc that performs the array matching operation. The Proc checks if any element in the array matches the condition. For empty conditions, returns FALSE_PROC.
36 37 38 39 40 |
# File 'lib/mongory/matchers/array_record_matcher.rb', line 36 def raw_proc return FALSE_PROC if matchers.empty? combine_procs(*matchers.map(&:to_proc)) end |