Class: Mongory::Matchers::ElemMatchMatcher

Inherits:
HashConditionMatcher show all
Defined in:
lib/mongory/matchers/elem_match_matcher.rb

Overview

ElemMatchMatcher implements the logic for Mongo-style $elemMatch.

It is used to determine if any element in an array matches the given condition.

This matcher delegates element-wise comparison to HashConditionMatcher, allowing nested conditions to be applied recursively.

Typically used internally by ArrayRecordMatcher when dealing with non-indexed hash-style subconditions.

Examples:

matcher = ElemMatchMatcher.build({ status: 'active' })
matcher.match?([{ status: 'inactive' }, { status: 'active' }]) #=> true

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 HashConditionMatcher

#matchers

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 a Hash.

Raises:



46
47
48
49
50
# File 'lib/mongory/matchers/elem_match_matcher.rb', line 46

def check_validity!
  raise TypeError, '$elemMatch needs a Hash.' unless @condition.is_a?(Hash)

  super
end

#priorityObject



38
39
40
# File 'lib/mongory/matchers/elem_match_matcher.rb', line 38

def priority
  3 + super
end

#raw_procProc

Creates a raw Proc that performs the element matching operation. The Proc checks if any element in the array matches the condition.

Returns:

  • (Proc)

    a Proc that performs the element matching operation



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mongory/matchers/elem_match_matcher.rb', line 25

def raw_proc
  super_proc = super
  need_convert = @context.need_convert
  data_converter = Mongory.data_converter

  Proc.new do |collection|
    collection.any? do |record|
      record = data_converter.convert(record) if need_convert
      super_proc.call(record)
    end
  end
end