Class: Mongory::Matchers::AbstractMatcher Abstract
- Inherits:
-
Object
- Object
- Mongory::Matchers::AbstractMatcher
- Includes:
- Utils
- Defined in:
- lib/mongory/matchers/abstract_matcher.rb
Overview
Subclasses must implement #match to define their matching logic
AbstractMatcher is the base class for all matchers in Mongory.
It defines a common interface (#match?
) and provides shared behavior
such as condition storage, optional conversion handling, and debugging output.
Each matcher is responsible for evaluating a specific type of condition against a record value. The base class provides infrastructure for:
- Condition validation
- Value conversion
- Debug output
- Proc caching
Direct Known Subclasses
AbstractMultiMatcher, EqMatcher, ExistsMatcher, GtMatcher, GteMatcher, InMatcher, LiteralMatcher, LtMatcher, LteMatcher, NeMatcher, NinMatcher, PresentMatcher, RegexMatcher
Constant Summary collapse
- KEY_NOT_FOUND =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Sentinel value used to represent missing keys when traversing nested hashes. This is used instead of nil to distinguish between missing keys and nil values.
SingletonBuilder.new('KEY_NOT_FOUND')
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
The raw condition this matcher was initialized with.
-
#context ⇒ Context
readonly
The query context containing configuration and current record.
Class Method Summary collapse
-
.define_matcher(name) { ... } ⇒ void
Defines a lazily-evaluated matcher accessor with instance-level caching.
Instance Method Summary collapse
-
#cached_proc ⇒ Proc
(also: #to_proc)
Converts the matcher into a Proc that can be used for matching.
-
#check_validity! ⇒ void
abstract
Validates the condition (no-op by default).
-
#debug_proc ⇒ Proc
Creates a debug-enabled version of the matcher proc.
-
#initialize(condition, context: Context.new) ⇒ AbstractMatcher
constructor
Initializes the matcher with a raw condition.
-
#match(record) ⇒ Boolean
abstract
Performs the actual match logic.
-
#match?(record) ⇒ Boolean
Matches the given record against the condition.
-
#raw_proc ⇒ Proc
Creates a raw Proc from the match method.
-
#render_tree(prefix = '', is_last: true) ⇒ void
Recursively prints the matcher structure into a formatted tree.
-
#uniq_key ⇒ String
Returns a unique key representing this matcher instance.
Methods included from Utils
included, included_classes, #is_blank?, #is_present?
Constructor Details
#initialize(condition, context: Context.new) ⇒ AbstractMatcher
Initializes the matcher with a raw condition.
76 77 78 79 80 81 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 76 def initialize(condition, context: Context.new) @condition = condition @context = context check_validity! end |
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the raw condition this matcher was initialized with.
58 59 60 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 58 def condition @condition end |
#context ⇒ Context (readonly)
Returns the query context containing configuration and current record.
61 62 63 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 61 def context @context end |
Class Method Details
.define_matcher(name) { ... } ⇒ void
This method returns an undefined value.
Defines a lazily-evaluated matcher accessor with instance-level caching. This is used to create cached accessors for submatcher instances.
53 54 55 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 53 def self.define_matcher(name, &block) define_instance_cache_method(:"#{name}_matcher", &block) end |
Instance Method Details
#cached_proc ⇒ Proc Also known as: to_proc
Converts the matcher into a Proc that can be used for matching. The Proc is cached for better performance.
98 99 100 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 98 def cached_proc @cached_proc ||= raw_proc end |
#check_validity! ⇒ void
This method returns an undefined value.
Validates the condition (no-op by default). Override in subclasses to raise error if invalid.
152 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 152 def check_validity!; end |
#debug_proc ⇒ Proc
Creates a debug-enabled version of the matcher proc. This version includes tracing and error handling.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 108 def debug_proc return @debug_proc if defined?(@debug_proc) raw_proc = raw_proc() @debug_proc = Proc.new do |record| result = nil Debugger.instance.with_indent do result = begin raw_proc.call(record) rescue StandardError => e e end debug_display(record, result) end result.is_a?(Exception) ? false : result end end |
#match(record) ⇒ Boolean
Performs the actual match logic. Subclasses must override this method.
144 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 144 def match(record); end |
#match?(record) ⇒ Boolean
Matches the given record against the condition. This method handles error cases and uses the cached proc for performance.
88 89 90 91 92 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 88 def match?(record) to_proc.call(record) rescue StandardError false end |
#raw_proc ⇒ Proc
Creates a raw Proc from the match method. This is used internally by to_proc and can be overridden by subclasses to provide custom matching behavior.
134 135 136 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 134 def raw_proc method(:match).to_proc end |
#render_tree(prefix = '', is_last: true) ⇒ void
This method returns an undefined value.
Recursively prints the matcher structure into a formatted tree. Supports indentation and branching layout using prefix symbols.
160 161 162 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 160 def render_tree(prefix = '', is_last: true) puts "#{prefix}#{is_last ? '└─ ' : '├─ '}#{tree_title}\n" end |
#uniq_key ⇒ String
Returns a unique key representing this matcher instance. Used for deduplication in multi-matchers.
68 69 70 |
# File 'lib/mongory/matchers/abstract_matcher.rb', line 68 def uniq_key "#{self.class}:condition:#{@condition.class}:#{@condition}" end |