Class: Mongory::Matchers::EveryMatcher

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

Overview

EveryMatcher implements the logic for Mongo-style $every which is not really support in MongoDB.

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

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

Examples:

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

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:



47
48
49
50
51
# File 'lib/mongory/matchers/every_matcher.rb', line 47

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

  super
end

#priorityObject



39
40
41
# File 'lib/mongory/matchers/every_matcher.rb', line 39

def priority
  3 + super
end

#raw_procProc

Note:

The proc includes error handling and context-based record conversion

Creates a raw Proc that performs the element matching operation. The Proc checks if all elements in the array match the condition.

Returns:

  • (Proc)

    A proc that performs element matching with context awareness



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

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

  Proc.new do |collection|
    next false unless collection.is_a?(Array)
    next false if collection.empty?

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