Class: Mongory::Matchers::FieldMatcher

Inherits:
LiteralMatcher show all
Defined in:
lib/mongory/matchers/field_matcher.rb

Overview

FieldMatcher handles field-level matching by extracting and comparing field values.

This matcher is responsible for:

  1. Extracting field values from records using dot notation
  2. Converting extracted values if needed
  3. Delegating the actual comparison to a submatcher

It supports:

  • Hash records with string/symbol keys
  • Array records with numeric indices
  • Objects that respond to []

Examples:

Basic field matching

matcher = FieldMatcher.build('age', 30)
matcher.match?({ 'age' => 30 })  #=> true
matcher.match?({ age: 30 })      #=> true

See Also:

Constant Summary collapse

CLASSES_NOT_ALLOW_TO_DIG =

A list of classes that should never be used for value digging. These typically respond to #[] but are semantically invalid for this context.

[
  ::String,
  ::Integer,
  ::Proc,
  ::Method,
  ::MatchData,
  ::Thread,
  ::Symbol
].freeze

Constants inherited from AbstractMatcher

AbstractMatcher::KEY_NOT_FOUND

Instance Attribute Summary

Attributes inherited from AbstractMatcher

#condition, #context

Instance Method Summary collapse

Methods inherited from LiteralMatcher

#array_record_matcher, #check_validity!, #dispatched_matcher, #render_tree

Methods inherited from AbstractMatcher

#cached_proc, #check_validity!, #debug_proc, define_matcher, #match, #match?, #render_tree

Methods included from Utils

included, included_classes, #is_blank?, #is_present?

Constructor Details

#initialize(field, condition, context: Context.new) ⇒ FieldMatcher

Initializes a new field matcher.

Parameters:

  • field (String, Symbol)

    the field to match against

  • condition (Object)

    the condition to match with

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

    the query context



41
42
43
44
# File 'lib/mongory/matchers/field_matcher.rb', line 41

def initialize(field, condition, context: Context.new)
  @field = field
  super(condition, context: context)
end

Instance Method Details

#priorityObject



112
113
114
# File 'lib/mongory/matchers/field_matcher.rb', line 112

def priority
  1 + super
end

#raw_procProc

Note:

The proc handles field extraction and delegates matching to the superclass

Creates a raw Proc that performs the field-based matching operation. The Proc extracts the field value and delegates matching to the superclass.

Returns:

  • (Proc)

    A proc that performs field-based matching with context awareness



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mongory/matchers/field_matcher.rb', line 84

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

  Proc.new do |record|
    sub_record =
      case record
      when Hash
        record.fetch(field) do
          record.fetch(field.to_sym, KEY_NOT_FOUND)
        end
      when Array
        record.fetch(field, KEY_NOT_FOUND)
      when KEY_NOT_FOUND, *CLASSES_NOT_ALLOW_TO_DIG
        next false
      else
        next false unless record.respond_to?(:[])

        record[field]
      end

    sub_record = data_converter.convert(sub_record) if need_convert
    super_proc.call(sub_record)
  end
end

#uniq_keyString

Returns a unique key for this matcher, including the field name. Used for deduplication in multi-matchers.

Returns:

  • (String)

    a unique key for this matcher

See Also:

  • AbstractMultiMatcher#matchers


121
122
123
# File 'lib/mongory/matchers/field_matcher.rb', line 121

def uniq_key
  super + "field:#{@field}"
end