Class: Mongory::Matchers::LiteralMatcher
- Inherits:
-
AbstractMatcher
- Object
- AbstractMatcher
- Mongory::Matchers::LiteralMatcher
- Defined in:
- lib/mongory/matchers/literal_matcher.rb
Overview
LiteralMatcher handles direct value comparison with special array handling.
This matcher is used when a condition is a literal value (not an operator). It handles both direct equality comparison and array-record scenarios.
For array records:
- Uses ArrayRecordMatcher to check if any element matches For non-array records:
- Uses appropriate matcher based on condition type (Hash, Regexp, nil, etc.)
Direct Known Subclasses
Constant Summary
Constants inherited from AbstractMatcher
AbstractMatcher::KEY_NOT_FOUND
Instance Attribute Summary
Attributes inherited from AbstractMatcher
Instance Method Summary collapse
-
#array_record_matcher ⇒ ArrayRecordMatcher
Lazily defines the collection matcher for array records.
-
#check_validity! ⇒ void
Validates the nested condition matcher, if applicable.
-
#dispatched_matcher ⇒ AbstractMatcher
Selects and returns the appropriate matcher instance for a given literal condition.
-
#raw_proc ⇒ Proc
Creates a raw Proc that performs the literal matching operation.
-
#render_tree(prefix = '', is_last: true) ⇒ void
Outputs the matcher tree by selecting either collection or condition matcher.
Methods inherited from AbstractMatcher
#cached_proc, #debug_proc, define_matcher, #initialize, #match, #match?, #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
#array_record_matcher ⇒ ArrayRecordMatcher
Lazily defines the collection matcher for array records.
92 93 94 |
# File 'lib/mongory/matchers/literal_matcher.rb', line 92 define_matcher(:array_record) do ArrayRecordMatcher.build(@condition, context: @context) end |
#check_validity! ⇒ void
This method returns an undefined value.
Validates the nested condition matcher, if applicable.
99 100 101 |
# File 'lib/mongory/matchers/literal_matcher.rb', line 99 def check_validity! dispatched_matcher.check_validity! end |
#dispatched_matcher ⇒ AbstractMatcher
Selects and returns the appropriate matcher instance for a given literal condition.
This method analyzes the type of the raw condition (e.g., Hash, Regexp, nil) and returns a dedicated matcher instance accordingly:
- Hash → dispatches to
HashConditionMatcher
- Regexp → dispatches to
RegexMatcher
- nil → dispatches to an
OrMatcher
that emulates MongoDB's{ field: nil }
behavior
For all other literal types, this method returns EqMatcher
, and fallback equality matching will be used.
This matcher is cached after the first invocation using define_instance_cache_method
to avoid unnecessary re-instantiation.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mongory/matchers/literal_matcher.rb', line 71 define_matcher(:dispatched) do case @condition when Hash HashConditionMatcher.build(@condition, context: @context) when Regexp RegexMatcher.build(@condition, context: @context) when nil OrMatcher.build([ { '$exists' => false }, { '$eq' => nil } ], context: @context) else EqMatcher.build(@condition, context: @context) end end |
#raw_proc ⇒ Proc
Creates a raw Proc that performs the literal matching operation. The Proc handles both array and non-array records appropriately.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mongory/matchers/literal_matcher.rb', line 37 def raw_proc array_record_proc = nil dispatched_proc = dispatched_matcher.to_proc Proc.new do |record| if record.is_a?(Array) array_record_proc ||= array_record_matcher.to_proc array_record_proc.call(record) else dispatched_proc.call(record) end end end |
#render_tree(prefix = '', is_last: true) ⇒ void
This method returns an undefined value.
Outputs the matcher tree by selecting either collection or condition matcher.
Delegates render_tree
to whichever submatcher was active.
109 110 111 112 113 114 |
# File 'lib/mongory/matchers/literal_matcher.rb', line 109 def render_tree(prefix = '', is_last: true) super target_matcher = @array_record_matcher || dispatched_matcher target_matcher.render_tree("#{prefix}#{is_last ? ' ' : '│ '}", is_last: true) end |