Class: Mongory::Matchers::AbstractMultiMatcher Abstract

Inherits:
AbstractMatcher show all
Defined in:
lib/mongory/matchers/abstract_multi_matcher.rb

Overview

This class is abstract.

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.

See Also:

Constant Summary collapse

TRUE_PROC =

A Proc that always returns true, used as a default for empty AND conditions

Returns:

  • (Proc)

    A proc that always returns true

Proc.new { |_| true }
FALSE_PROC =

A Proc that always returns false, used as a default for empty OR conditions

Returns:

  • (Proc)

    A proc that always returns false

Proc.new { |_| false }

Constants inherited from AbstractMatcher

Mongory::Matchers::AbstractMatcher::KEY_NOT_FOUND

Instance Attribute Summary

Attributes inherited from AbstractMatcher

#condition, #context

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • args (Array)

    arguments passed to the constructor

  • context (Context) (defaults to: Context.new)

    the query context

Returns:

  • (AbstractMatcher)

    the constructed matcher or its unwrapped submatcher

[View source]

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.

Raises:

[View source]

59
60
61
# File 'lib/mongory/matchers/abstract_multi_matcher.rb', line 59

def check_validity!
  matchers.each(&:check_validity!)
end

#priorityObject

[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.

Parameters:

  • prefix (String) (defaults to: '')

    current line prefix for tree alignment

  • is_last (Boolean) (defaults to: true)

    whether this node is the last sibling

[View source]

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