Class: Mongory::Matchers::AbstractMultiMatcher Abstract
- Inherits:
-
AbstractMatcher
- Object
- AbstractMatcher
- Mongory::Matchers::AbstractMultiMatcher
- Defined in:
- lib/mongory/matchers/abstract_multi_matcher.rb
Overview
AbstractMultiMatcher is an abstract base class for matchers that operate
on multiple subconditions. It provides a generic match loop that applies
a logical operator (e.g., all?
, any?
) over a list of sub-matchers.
Subclasses must define two methods:
#build_sub_matcher
: how to construct a matcher from each condition#operator
: which enumerator method to use (e.g., :all?, :any?)
Sub-matchers are cached using define_instance_cache_method
to prevent
repeated construction.
Direct Known Subclasses
AndMatcher, ArrayRecordMatcher, HashConditionMatcher, OrMatcher
Constant Summary collapse
- TRUE_PROC =
A Proc that always returns true, used as a default for empty AND conditions
Proc.new { |_| true }
- FALSE_PROC =
A Proc that always returns false, used as a default for empty OR conditions
Proc.new { |_| false }
Constants inherited from AbstractMatcher
Mongory::Matchers::AbstractMatcher::KEY_NOT_FOUND
Instance Attribute Summary
Attributes inherited from AbstractMatcher
Class Method Summary collapse
-
.build_or_unwrap(*args, context: Context.new) ⇒ AbstractMatcher
Builds a matcher and conditionally unwraps it.
Instance Method Summary collapse
-
#check_validity! ⇒ void
Recursively checks all submatchers for validity.
- #priority ⇒ Object
-
#render_tree(prefix = '', is_last: true) ⇒ void
Overrides base render_tree to recursively print all submatchers.
Methods inherited from AbstractMatcher
#cached_proc, #debug_proc, define_matcher, #initialize, #match, #match?, #raw_proc, #uniq_key
Methods included from Utils
included, included_classes, #is_blank?, #is_present?
Constructor Details
This class inherits a constructor from Mongory::Matchers::AbstractMatcher
Class Method Details
.build_or_unwrap(*args, context: Context.new) ⇒ AbstractMatcher
Builds a matcher and conditionally unwraps it. If unwrapping is enabled and there is only one submatcher, returns that submatcher instead of the multi-matcher wrapper.
46 47 48 49 50 51 52 |
# File 'lib/mongory/matchers/abstract_multi_matcher.rb', line 46 def self.build_or_unwrap(*args, context: Context.new) matcher = new(*args, context: context) return matcher unless @enable_unwrap matcher = matcher.matchers.first if matcher.matchers.count == 1 matcher end |
Instance Method Details
#check_validity! ⇒ void
This method returns an undefined value.
Recursively checks all submatchers for validity. Raises an error if any submatcher is invalid.
59 60 61 |
# File 'lib/mongory/matchers/abstract_multi_matcher.rb', line 59 def check_validity! matchers.each(&:check_validity!) end |
#priority ⇒ Object
[View source]
79 80 81 |
# File 'lib/mongory/matchers/abstract_multi_matcher.rb', line 79 def priority 1 + matchers.sum(&:priority) end |
#render_tree(prefix = '', is_last: true) ⇒ void
This method returns an undefined value.
Overrides base render_tree to recursively print all submatchers. Each child matcher will be displayed under this multi-matcher node.
69 70 71 72 73 74 75 76 77 |
# File 'lib/mongory/matchers/abstract_multi_matcher.rb', line 69 def render_tree(prefix = '', is_last: true) super new_prefix = "#{prefix}#{is_last ? ' ' : '│ '}" last_index = matchers.count - 1 matchers.each_with_index do |matcher, index| matcher.render_tree(new_prefix, is_last: index == last_index) end end |