Class: Mongory::Matchers::FieldMatcher
- Inherits:
-
LiteralMatcher
- Object
- AbstractMatcher
- LiteralMatcher
- Mongory::Matchers::FieldMatcher
- 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:
- Extracting field values from records using dot notation
- Converting extracted values if needed
- Delegating the actual comparison to a submatcher
It supports:
- Hash records with string/symbol keys
- Array records with numeric indices
- Objects that respond to
[]
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
Instance Method Summary collapse
-
#initialize(field, condition, context: Context.new) ⇒ FieldMatcher
constructor
Initializes a new field matcher.
- #priority ⇒ Object
-
#raw_proc ⇒ Proc
Creates a raw Proc that performs the field-based matching operation.
-
#uniq_key ⇒ String
Returns a unique key for this matcher, including the field name.
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.
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
#priority ⇒ Object
112 113 114 |
# File 'lib/mongory/matchers/field_matcher.rb', line 112 def priority 1 + super end |
#raw_proc ⇒ Proc
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.
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_key ⇒ String
Returns a unique key for this matcher, including the field name. Used for deduplication in multi-matchers.
121 122 123 |
# File 'lib/mongory/matchers/field_matcher.rb', line 121 def uniq_key super + "field:#{@field}" end |