Class: Mongory::Converters::KeyConverter

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

Overview

KeyConverter handles transformation of field keys in query conditions. It normalizes symbol keys into string paths, splits dotted keys, and delegates to the appropriate converter logic.

This class inherits from AbstractConverter and provides specialized handling for different key types:

  • String keys with dots are split into nested paths
  • Symbol keys are converted to strings
  • QueryOperator instances are handled via their DSL hooks
  • Other types fall back to the parent converter

Used by ConditionConverter to build query structures from flat input.

  • "a.b.c" => v becomes { "a" => { "b" => { "c" => v } } }
  • Symbols are stringified and delegated to String logic
  • QueryOperator dispatches to internal DSL hook

Examples:

Convert a dotted string key

KeyConverter.instance.convert("user.name", "John")
# => { "user" => { "name" => "John" } }

Convert a symbol key

KeyConverter.instance.convert(:status, "active")
# => { "status" => "active" }

See Also:

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

#convert(target, other) ⇒ Hash

Converts a key into its normalized form based on its type. Handles strings, symbols, and QueryOperator instances. Falls back to parent converter for other types.

Parameters:

  • target (Object)

    the key to convert

  • other (Object)

    the value associated with the key

Returns:

  • (Hash)

    the converted key-value pair



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mongory/converters/key_converter.rb', line 41

def convert(target, other)
  case target
  when String
    convert_string_key(target, other)
  when Symbol
    convert_string_key(target.to_s, other)
  when QueryOperator
    # Handle special case for QueryOperator
    convert_string_key(*target.__expr_part__(other).first)
  else
    super_convert(target, other)
  end
end

#convert_string_key(key, value) ⇒ Hash

Converts a dotted string key into nested hash form. Splits the key on dots and builds a nested structure. Handles escaped dots in the key.

Parameters:

  • key (String)

    the dotted key string, e.g. "a.b.c"

  • value (Object)

    the value to assign at the deepest level

Returns:

  • (Hash)

    nested hash structure



66
67
68
69
70
71
72
73
74
75
# File 'lib/mongory/converters/key_converter.rb', line 66

def convert_string_key(key, value)
  ret = {}
  *sub_keys, last_key = key.split(/(?<!\\)\./)
  last_hash = sub_keys.reduce(ret) do |res, sub_key|
    next_res = res[normalize_key(sub_key)] = {}
    next_res
  end
  last_hash[normalize_key(last_key)] = value
  ret
end

#fallback(target, other) ⇒ Object



55
56
57
# File 'lib/mongory/converters/key_converter.rb', line 55

def fallback(target, other)
  { target => other }
end

#normalize_key(key) ⇒ String

Normalizes a key by unescaping escaped dots. This allows for literal dots in field names.

Parameters:

  • key (String)

    the key to normalize

Returns:

  • (String)

    the normalized key



82
83
84
# File 'lib/mongory/converters/key_converter.rb', line 82

def normalize_key(key)
  key.gsub(/\\\./, '.')
end

#super_convertObject



32
# File 'lib/mongory/converters/key_converter.rb', line 32

alias_method :super_convert, :convert