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!, #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.

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



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

#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:



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_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
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