Class: Mongory::Matchers::ArrayRecordMatcher

Inherits:
AbstractMultiMatcher show all
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).

Examples:

Match exact array

matcher = ArrayRecordMatcher.build([1, 2, 3])
matcher.match?([1, 2, 3])  #=> true
matcher.match?([1, 2])     #=> false

Match with hash condition

matcher = ArrayRecordMatcher.build({ '$gt' => 5 })
matcher.match?([3, 6, 9])  #=> true (6 and 9 match)
matcher.match?([1, 2, 3])  #=> false

Empty conditions

matcher = ArrayRecordMatcher.build([])
matcher.match?(record) #=> false (uses FALSE_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, #check_validity!, #priority, #render_tree

Methods inherited from AbstractMatcher

#cached_proc, #check_validity!, #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

#matchersArray<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 $elemMatch matcher for element-wise comparison

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mongory/matchers/array_record_matcher.rb', line 48

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.sort_by(&:priority)
end

#raw_procProc

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.

Returns:

  • (Proc)

    a Proc that performs the array matching operation



36
37
38
# File 'lib/mongory/matchers/array_record_matcher.rb', line 36

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