Class: Mongory::QueryMatcher

Inherits:
Matchers::HashConditionMatcher show all
Defined in:
lib/mongory/query_matcher.rb

Overview

The top-level matcher for compiled query conditions.

Delegates to Matchers::LiteralMatcher after transforming input via Converters::ConditionConverter.

Typically used internally by QueryBuilder.

Conversion via Mongory.data_converter is applied to the record before matching to ensure consistent data types.

Examples:

Basic matching

matcher = QueryMatcher.new({ :age.gte => 18 })
matcher.match?(record) # => true/false

Complex condition

matcher = QueryMatcher.new({
  :age.gte => 18,
  :$or => [
    { :name => /J/ },
    { :name.eq => 'Bob' }
  ]
})

See Also:

Constant Summary

Constants inherited from Matchers::AbstractMultiMatcher

Matchers::AbstractMultiMatcher::FALSE_PROC, Matchers::AbstractMultiMatcher::TRUE_PROC

Constants inherited from Matchers::AbstractMatcher

Matchers::AbstractMatcher::KEY_NOT_FOUND

Instance Attribute Summary

Attributes inherited from Matchers::AbstractMatcher

#condition, #context

Instance Method Summary collapse

Methods inherited from Matchers::HashConditionMatcher

#matchers

Methods inherited from Matchers::AbstractMultiMatcher

build_or_unwrap, #priority

Methods inherited from Matchers::AbstractMatcher

#cached_proc, #debug_proc, define_matcher, #match, #match?, #priority, #uniq_key

Methods included from Utils

included, included_classes, #is_blank?, #is_present?

Constructor Details

#initialize(condition, context: Utils::Context.new) ⇒ QueryMatcher

Initializes a new query matcher with the given condition. The condition is converted using Mongory.condition_converter before being passed to the parent matcher.

Parameters:

  • condition (Hash<Symbol, Object>)

    a query condition using operator-style symbol keys, e.g. { :age.gt => 18 }, which will be parsed by Mongory.condition_converter.

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

    The query context containing configuration and current record

Options Hash (context:):

  • :config (Hash)

    The query configuration

  • :current_record (Object)

    The current record being processed

  • :need_convert (Boolean)

    Whether the record needs to be converted



42
43
44
# File 'lib/mongory/query_matcher.rb', line 42

def initialize(condition, context: Utils::Context.new)
  super(Mongory.condition_converter.convert(condition), context: context)
end

Instance Method Details

#check_validity!void

This method returns an undefined value.

Overrides the parent class's check_validity! to prevent premature matcher tree construction. This matcher does not require validation, so this is a no-op.



89
90
91
# File 'lib/mongory/query_matcher.rb', line 89

def check_validity!
  # No-op for this matcher
end

#prepare_queryvoid

This method returns an undefined value.

Prepares the query for execution by ensuring all necessary matchers are initialized. This is called before query execution to avoid premature matcher tree construction.



83
# File 'lib/mongory/query_matcher.rb', line 83

alias_method :prepare_query, :check_validity!

#raw_procProc

Note:

The proc includes error handling and context-based record conversion

Returns a Proc that can be used for fast matching. The Proc converts the record using Mongory.data_converter and delegates to the superclass's raw_proc.

Returns:

  • (Proc)

    A proc that performs query matching with context awareness



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mongory/query_matcher.rb', line 52

def raw_proc
  super_proc = super
  need_convert = @context.need_convert
  data_converter = Mongory.data_converter

  Proc.new do |record|
    record = data_converter.convert(record) if need_convert
    super_proc.call(record)
  rescue StandardError
    false
  end
end

#render_treevoid

This method returns an undefined value.

Renders the full matcher tree for the current query. This method is intended to be the public entry point for rendering the matcher tree. It does not accept any arguments and internally handles rendering via the configured pretty-print logic.

Subclasses or internal matchers should implement their own #render_tree(prefix, is_last:) for internal recursion.



75
76
77
# File 'lib/mongory/query_matcher.rb', line 75

def render_tree
  super
end