Class: Mongory::Converters::ConditionConverter

Inherits:
AbstractConverter show all
Defined in:
lib/mongory/converters/condition_converter.rb

Overview

ConditionConverter transforms a flat condition hash into nested hash form. This is used internally by ValueConverter to normalize condition structures like { "foo.bar" => 1, "foo.baz" => 2 } into nested Mongo-style conditions. Used by QueryMatcher to normalize condition input for internal matching.

Combines key transformation (via KeyConverter) and value normalization (via ValueConverter), and merges overlapping keys.

Examples:

Convert condition hash

ConditionConverter.instance.convert({ "a.b" => 1, "a.c" => 2 })
# => { "a" => { "b" => 1, "c" => 2 } }

Constant Summary

Constants inherited from AbstractConverter

AbstractConverter::NOTHING

Instance Method Summary collapse

Methods inherited from AbstractConverter

#configure, #fallback, #find_strategy, #initialize, #register

Methods inherited from Utils::SingletonBuilder

#initialize, #inspect, #to_s

Constructor Details

This class inherits a constructor from Mongory::Converters::AbstractConverter

Instance Method Details

#convert(condition) ⇒ Hash

Converts a flat condition hash into a nested structure. Applies both key and value conversion, and merges overlapping keys.

Parameters:

  • condition (Hash)

    the flat condition hash to convert

Returns:

  • (Hash)

    the transformed nested condition



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongory/converters/condition_converter.rb', line 23

def convert(condition)
  return condition if condition.is_a?(Converted)

  result = Converted::Hash.new
  condition.each_pair do |k, v|
    converted_value = value_converter.convert(v)
    converted_pair = key_converter.convert(k, converted_value)
    result.deep_merge!(converted_pair)
  end

  result
end

#freezevoid

This method returns an undefined value.

Freezes internal converters to prevent further modification.



55
56
57
58
59
# File 'lib/mongory/converters/condition_converter.rb', line 55

def freeze
  super
  key_converter.freeze
  value_converter.freeze
end

#key_converterAbstractConverter

Note:

Singleton instance, not configurable after initialization

Returns the key converter used to transform condition keys.

Returns:



40
41
42
# File 'lib/mongory/converters/condition_converter.rb', line 40

def key_converter
  KeyConverter.instance
end

#value_converterAbstractConverter

Note:

Singleton instance, not configurable after initialization

Returns the value converter used to transform condition values.

Returns:



48
49
50
# File 'lib/mongory/converters/condition_converter.rb', line 48

def value_converter
  ValueConverter.instance
end