Class: Mongory::Matchers::AndMatcher
- Inherits:
-
AbstractMultiMatcher
- Object
- AbstractMatcher
- AbstractMultiMatcher
- Mongory::Matchers::AndMatcher
- Defined in:
- lib/mongory/matchers/and_matcher.rb
Overview
AndMatcher implements the $and
logical operator.
It evaluates an array of subconditions and returns true only if all of them match. For empty conditions, it returns true (using TRUE_PROC), following MongoDB's behavior.
Unlike other matchers, AndMatcher flattens the underlying matcher tree by
delegating each subcondition to a HashConditionMatcher
, and further extracting
all inner matchers. Duplicate matchers are deduplicated by uniq_key
.
This allows the matcher trace (.explain
) to render as a flat list of independent conditions.
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
Instance Method Summary collapse
-
#check_validity! ⇒ void
Ensures the condition is an array of hashes.
-
#matchers ⇒ Array<AbstractMatcher>
Returns the flattened list of all matchers from each subcondition.
-
#raw_proc ⇒ Proc
Creates a raw Proc that performs the AND operation.
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.
59 60 61 62 63 64 65 66 67 |
# File 'lib/mongory/matchers/and_matcher.rb', line 59 def check_validity! raise TypeError, '$and needs an array' unless @condition.is_a?(Array) @condition.each do |sub_condition| raise TypeError, '$and needs an array of hash' unless sub_condition.is_a?(Hash) end super end |
#matchers ⇒ Array<AbstractMatcher>
Returns the flattened list of all matchers from each subcondition.
Each condition is passed to a HashConditionMatcher, then recursively flattened.
All matchers are then deduplicated using uniq_key
.
49 50 51 52 53 |
# File 'lib/mongory/matchers/and_matcher.rb', line 49 define_instance_cache_method(:matchers) do @condition.flat_map do |condition| HashConditionMatcher.new(condition, context: @context).matchers end.uniq(&:uniq_key).sort_by(&:priority) end |
#raw_proc ⇒ Proc
Creates a raw Proc that performs the AND operation. The Proc combines all subcondition Procs and returns true only if all match. For empty conditions, returns TRUE_PROC.
38 39 40 |
# File 'lib/mongory/matchers/and_matcher.rb', line 38 def raw_proc combine_procs_with_and(*matchers.map(&:to_proc)) end |