Module: Tins::HashSymbolizeKeysRecursive
- Extended by:
- ThreadLocal
- Defined in:
- lib/tins/hash_symbolize_keys_recursive.rb
Overview
This module provides recursive symbolization of hash keys. It handles nested structures including hashes and arrays, with special handling for circular references to prevent infinite recursion.
Instance Method Summary collapse
-
#_transform_keys_recursive(object, circular: nil, transform:) ⇒ Object
private
Performs the actual recursive transformation work.
-
#stringify_keys_recursive(circular: nil) ⇒ Hash
Converts all keys in the hash (and nested hashes) to strings.
-
#stringify_keys_recursive!(circular: nil) ⇒ Hash
Converts all keys in the hash (and nested hashes) to strings in place.
-
#symbolize_keys_recursive(circular: nil) ⇒ Hash, ...
Recursively converts all string keys in a hash (and nested hashes/arrays) to symbols.
-
#symbolize_keys_recursive!(circular: nil) ⇒ Hash, ...
Recursively converts all string keys in a hash (and nested hashes/arrays) to symbols.
Methods included from ThreadLocal
instance_thread_local, thread_local
Instance Method Details
#_transform_keys_recursive(object, circular: nil, transform:) ⇒ Object (private)
Performs the actual recursive transformation work
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/tins/hash_symbolize_keys_recursive.rb', line 110 def _transform_keys_recursive(object, circular: nil, transform:) case when seen[object.__id__] object = circular when object.respond_to?(:to_hash) object = object.to_hash seen[object.__id__] = true new_object = object.class.new seen[new_object.__id__] = true object.each do |k, v| new_object[k.to_s.__send__(transform)] = _transform_keys_recursive(v, circular: circular, transform:) end object = new_object when object.respond_to?(:to_ary) object = object.to_ary seen[object.__id__] = true new_object = object.class.new(object.size) seen[new_object.__id__] = true object.each_with_index do |v, i| new_object[i] = _transform_keys_recursive(v, circular: circular, transform:) end object = new_object end object end |
#stringify_keys_recursive(circular: nil) ⇒ Hash
Converts all keys in the hash (and nested hashes) to strings.
66 67 68 69 70 71 |
# File 'lib/tins/hash_symbolize_keys_recursive.rb', line 66 def stringify_keys_recursive(circular: nil) self.seen = {} _transform_keys_recursive(self, circular: circular, transform: :to_s) ensure self.seen = nil end |
#stringify_keys_recursive!(circular: nil) ⇒ Hash
Converts all keys in the hash (and nested hashes) to strings in place.
98 99 100 |
# File 'lib/tins/hash_symbolize_keys_recursive.rb', line 98 def stringify_keys_recursive!(circular: nil) replace stringify_keys_recursive(circular: circular) end |
#symbolize_keys_recursive(circular: nil) ⇒ Hash, ...
Recursively converts all string keys in a hash (and nested hashes/arrays) to symbols. This method does not modify the original hash.
50 51 52 53 54 55 |
# File 'lib/tins/hash_symbolize_keys_recursive.rb', line 50 def symbolize_keys_recursive(circular: nil) self.seen = {} _transform_keys_recursive(self, circular: circular, transform: :to_sym) ensure self.seen = nil end |
#symbolize_keys_recursive!(circular: nil) ⇒ Hash, ...
Recursively converts all string keys in a hash (and nested hashes/arrays) to symbols. This method modifies the original hash in place.
85 86 87 |
# File 'lib/tins/hash_symbolize_keys_recursive.rb', line 85 def symbolize_keys_recursive!(circular: nil) replace symbolize_keys_recursive(circular: circular) end |