Class: Mongory::Converters::ValueConverter

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

Overview

ValueConverter transforms query values into a standardized form. It handles arrays, hashes, regex, and basic types, and delegates fallback logic to DataConverter. Used by ConditionConverter to prepare values in nested queries.

  • Arrays are recursively converted
  • Hashes are interpreted as nested conditions
  • Regex becomes a Mongo-style $regex hash
  • Strings and Integers are passed through
  • Everything else falls back to DataConverter

Examples:

Convert a regex

ValueConverter.instance.convert(/foo/) #=> { "$regex" => "foo" }

Constant Summary

Constants inherited from AbstractConverter

AbstractConverter::NOTHING

Instance Method Summary collapse

Methods inherited from AbstractConverter

#configure, #find_strategy, #freeze, #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

#condition_converterConditionConverter

Returns the condition converter instance.

Returns:



47
48
49
# File 'lib/mongory/converters/value_converter.rb', line 47

def condition_converter
  @condition_converter ||= Mongory.condition_converter
end

#convert(target) ⇒ Object

Converts a value into its standardized form based on its type. Handles arrays, hashes, regex, and basic types.

Parameters:

  • target (Object)

    the value to convert

Returns:

  • (Object)

    the converted value



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongory/converters/value_converter.rb', line 27

def convert(target)
  case target
  when String, Integer, Regexp, Converted
    target
  when Array
    Converted::Array.new(target.map { |x| convert(x) })
  when Hash
    condition_converter.convert(target)
  else
    super_convert(target)
  end
end

#fallback(target) ⇒ Object



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

def fallback(target, *)
  Mongory.data_converter.convert(target)
end

#super_convertObject



20
# File 'lib/mongory/converters/value_converter.rb', line 20

alias_method :super_convert, :convert