Class: Mongory::Matchers::OrMatcher

Inherits:
AbstractMultiMatcher show all
Defined in:
lib/mongory/matchers/or_matcher.rb

Overview

OrMatcher implements the $or logical operator.

It evaluates an array of subconditions and returns true if any one of them matches. For empty conditions, it returns false (using FALSE_PROC).

Each subcondition is handled by a HashConditionMatcher with conversion disabled, since the parent matcher already manages data conversion.

This matcher inherits submatcher dispatch and evaluation logic from AbstractMultiMatcher.

Examples:

matcher = OrMatcher.build([
  { age: { :$lt => 18 } },
  { admin: true }
])
matcher.match?(record) #=> true if either condition matches

Empty conditions

matcher = OrMatcher.build([])
matcher.match?(record) #=> false (uses FALSE_PROC)

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 AbstractMultiMatcher

build_or_unwrap, #priority, #render_tree

Methods inherited from AbstractMatcher

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

#check_validity!void

This method returns an undefined value.

Ensures the condition is an array of hashes.

Raises:



55
56
57
58
59
60
61
62
63
# File 'lib/mongory/matchers/or_matcher.rb', line 55

def check_validity!
  raise TypeError, '$or needs an array' unless @condition.is_a?(Array)

  @condition.each do |sub_condition|
    raise TypeError, '$or needs an array of hash' unless sub_condition.is_a?(Hash)
  end

  super
end

#matchersArray<AbstractMatcher>

Builds an array of matchers from the subconditions. Each subcondition is wrapped in a HashConditionMatcher.

Returns:



45
46
47
48
49
# File 'lib/mongory/matchers/or_matcher.rb', line 45

define_instance_cache_method(:matchers) do
  @condition.map do |condition|
    HashConditionMatcher.build(condition, context: @context)
  end.sort_by(&:priority)
end

#raw_procProc

Creates a raw Proc that performs the or-matching operation. The Proc combines all submatcher Procs and returns true if any match. For empty conditions, returns FALSE_PROC.

Returns:

  • (Proc)

    a Proc that performs the or-matching operation



37
38
39
# File 'lib/mongory/matchers/or_matcher.rb', line 37

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