Class: Mongory::Matchers::LiteralMatcher

Inherits:
AbstractMatcher show all
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.)

Examples:

Basic equality matching

matcher = LiteralMatcher.build(42)
matcher.match?(42)       #=> true
matcher.match?([42, 43]) #=> true (array contains 42)

Regexp matching

matcher = LiteralMatcher.build(/foo/)
matcher.match?("foo")     #=> true
matcher.match?(["foobar"]) #=> true

Hash condition matching

matcher = LiteralMatcher.build({ '$gt' => 10 })
matcher.match?(15)        #=> true
matcher.match?([5, 15])   #=> true

See Also:

Direct Known Subclasses

FieldMatcher, NotMatcher, SizeMatcher

Constant Summary

Constants inherited from AbstractMatcher

AbstractMatcher::KEY_NOT_FOUND

Instance Attribute Summary

Attributes inherited from AbstractMatcher

#condition, #context

Instance Method Summary collapse

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_matcherArrayRecordMatcher

Lazily defines the collection matcher for array records.

Returns:

See Also:



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_matcherAbstractMatcher

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.

Returns:

See Also:



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_procProc

Creates a raw Proc that performs the literal matching operation. The Proc handles both array and non-array records appropriately.

Returns:

  • (Proc)

    a Proc that performs the literal matching operation



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.

Parameters:

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

    the prefix string for tree rendering

  • is_last (Boolean) (defaults to: true)

    whether this is the last node in the tree



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