Class: Mongory::Converters::KeyConverter
- Inherits:
-
AbstractConverter
- Object
- Utils::SingletonBuilder
- AbstractConverter
- Mongory::Converters::KeyConverter
- 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
Constant Summary
Constants inherited from AbstractConverter
Instance Method Summary collapse
-
#convert(target, other) ⇒ Hash
Converts a key into its normalized form based on its type.
-
#convert_string_key(key, value) ⇒ Hash
Converts a dotted string key into nested hash form.
- #fallback(target, other) ⇒ Object
-
#normalize_key(key) ⇒ String
Normalizes a key by unescaping escaped dots.
- #super_convert ⇒ Object
Methods inherited from AbstractConverter
#configure, #find_strategy, #freeze, #initialize, #register
Methods inherited from Utils::SingletonBuilder
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.
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.
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.
82 83 84 |
# File 'lib/mongory/converters/key_converter.rb', line 82 def normalize_key(key) key.gsub(/\\\./, '.') end |
#super_convert ⇒ Object
32 |
# File 'lib/mongory/converters/key_converter.rb', line 32 alias_method :super_convert, :convert |