Module: Transproc::HashTransformations
- Extended by:
- Functions
- Defined in:
- lib/transproc/hash.rb
Overview
Transformation functions for Hash objects
Instance Method Summary collapse
-
#map_keys(hash, fn) ⇒ Hash
Map all keys in a hash with the provided transformation function.
-
#map_keys!(hash, fn) ⇒ Object
Same as ‘:map_keys` but mutates the hash.
-
#map_value(hash, key, fn) ⇒ Hash
Map a key in a hash with the provided transformation function.
-
#map_value!(hash, key, fn) ⇒ Object
Same as ‘:map_value` but mutates the hash.
-
#map_values(hash, fn) ⇒ Hash
Map all values in a hash using transformation function.
-
#map_values!(hash, fn) ⇒ Hash
Same as ‘:map_values` but mutates the hash.
-
#nest(hash, key, keys) ⇒ Hash
Nest values from specified keys under a new key.
-
#nest!(hash, root, keys) ⇒ Object
Same as ‘:nest` but mutates the hash.
-
#rename_keys(hash, mapping) ⇒ Hash
Rename all keys in a hash using provided mapping hash.
-
#rename_keys!(hash, mapping) ⇒ Object
Same as ‘:rename_keys` but mutates the hash.
-
#stringify_keys(hash) ⇒ Hash
Stringify all keys in a hash.
-
#stringify_keys!(hash) ⇒ Object
Same as ‘:stringify_keys` but mutates the hash.
-
#symbolize_keys(hash) ⇒ Hash
Symbolize all keys in a hash.
-
#symbolize_keys!(hash) ⇒ Object
Same as ‘:symbolize_keys` but mutates the hash.
-
#unwrap(hash, root, keys) ⇒ Hash
Collapse a nested hash from a specified key.
-
#unwrap!(hash, root, keys = nil) ⇒ Object
Same as ‘:unwrap` but mutates the hash.
Methods included from Functions
Instance Method Details
#map_keys(hash, fn) ⇒ Hash
Map all keys in a hash with the provided transformation function
31 32 33 |
# File 'lib/transproc/hash.rb', line 31 def map_keys(hash, fn) map_keys!(Hash[hash], fn) end |
#map_keys!(hash, fn) ⇒ Object
Same as ‘:map_keys` but mutates the hash
40 41 42 43 |
# File 'lib/transproc/hash.rb', line 40 def map_keys!(hash, fn) hash.keys.each { |key| hash[fn[key]] = hash.delete(key) } hash end |
#map_value(hash, key, fn) ⇒ Hash
Map a key in a hash with the provided transformation function
159 160 161 |
# File 'lib/transproc/hash.rb', line 159 def map_value(hash, key, fn) hash.merge(key => fn[hash[key]]) end |
#map_value!(hash, key, fn) ⇒ Object
Same as ‘:map_value` but mutates the hash
168 169 170 |
# File 'lib/transproc/hash.rb', line 168 def map_value!(hash, key, fn) hash.update(key => fn[hash[key]]) end |
#map_values(hash, fn) ⇒ Hash
Map all values in a hash using transformation function
104 105 106 |
# File 'lib/transproc/hash.rb', line 104 def map_values(hash, fn) map_values!(Hash[hash], fn) end |
#map_values!(hash, fn) ⇒ Hash
Same as ‘:map_values` but mutates the hash
117 118 119 120 |
# File 'lib/transproc/hash.rb', line 117 def map_values!(hash, fn) hash.each { |key, value| hash[key] = fn[value] } hash end |
#nest(hash, key, keys) ⇒ Hash
Nest values from specified keys under a new key
183 184 185 |
# File 'lib/transproc/hash.rb', line 183 def nest(hash, key, keys) nest!(Hash[hash], key, keys) end |
#nest!(hash, root, keys) ⇒ Object
Same as ‘:nest` but mutates the hash
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/transproc/hash.rb', line 192 def nest!(hash, root, keys) nest_keys = hash.keys & keys if nest_keys.size > 0 child = Hash[nest_keys.zip(nest_keys.map { |key| hash.delete(key) })] hash.update(root => child) else hash.update(root => {}) end end |
#rename_keys(hash, mapping) ⇒ Hash
Rename all keys in a hash using provided mapping hash
134 135 136 |
# File 'lib/transproc/hash.rb', line 134 def rename_keys(hash, mapping) rename_keys!(Hash[hash], mapping) end |
#rename_keys!(hash, mapping) ⇒ Object
Same as ‘:rename_keys` but mutates the hash
143 144 145 146 |
# File 'lib/transproc/hash.rb', line 143 def rename_keys!(hash, mapping) mapping.each { |k, v| hash[v] = hash.delete(k) } hash end |
#stringify_keys(hash) ⇒ Hash
Stringify all keys in a hash
80 81 82 |
# File 'lib/transproc/hash.rb', line 80 def stringify_keys(hash) stringify_keys!(Hash[hash]) end |
#stringify_keys!(hash) ⇒ Object
Same as ‘:stringify_keys` but mutates the hash
89 90 91 |
# File 'lib/transproc/hash.rb', line 89 def stringify_keys!(hash) map_keys!(hash, Transproc(:to_string).fn) end |
#symbolize_keys(hash) ⇒ Hash
Symbolize all keys in a hash
56 57 58 |
# File 'lib/transproc/hash.rb', line 56 def symbolize_keys(hash) symbolize_keys!(Hash[hash]) end |
#symbolize_keys!(hash) ⇒ Object
Same as ‘:symbolize_keys` but mutates the hash
65 66 67 |
# File 'lib/transproc/hash.rb', line 65 def symbolize_keys!(hash) map_keys!(hash, Transproc(:to_symbol).fn) end |
#unwrap(hash, root, keys) ⇒ Hash
Collapse a nested hash from a specified key
214 215 216 217 |
# File 'lib/transproc/hash.rb', line 214 def unwrap(hash, root, keys) copy = Hash[hash].merge(root => Hash[hash[root]]) unwrap!(copy, root, keys) end |
#unwrap!(hash, root, keys = nil) ⇒ Object
Same as ‘:unwrap` but mutates the hash
224 225 226 227 228 229 230 231 232 |
# File 'lib/transproc/hash.rb', line 224 def unwrap!(hash, root, keys = nil) if nested_hash = hash[root] keys ||= nested_hash.keys hash.update(Hash[keys.zip(keys.map { |key| nested_hash.delete(key) })]) hash.delete(root) if nested_hash.empty? end hash end |