Module: Transproc::HashTransformations
- Extended by:
- Functions
- Defined in:
- lib/transproc/hash.rb
Overview
Transformation functions for Hash objects
Instance Method Summary collapse
-
#accept_keys(hash, keys) ⇒ Hash
Accepts specified keys from a hash.
-
#accept_keys!(hash, keys) ⇒ Object
Same as ‘:accept_keys` but mutates the hash.
-
#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.
-
#reject_keys(hash, keys) ⇒ Hash
Rejects specified keys from a hash.
-
#reject_keys!(hash, keys) ⇒ Object
Same as ‘:reject_keys` 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 = nil) ⇒ 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
#accept_keys(hash, keys) ⇒ Hash
Accepts specified keys from a hash
194 195 196 |
# File 'lib/transproc/hash.rb', line 194 def accept_keys(hash, keys) accept_keys!(Hash[hash], keys) end |
#accept_keys!(hash, keys) ⇒ Object
Same as ‘:accept_keys` but mutates the hash
178 179 180 |
# File 'lib/transproc/hash.rb', line 178 def accept_keys!(hash, keys) reject_keys!(hash, hash.keys - keys) end |
#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
209 210 211 |
# File 'lib/transproc/hash.rb', line 209 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
218 219 220 |
# File 'lib/transproc/hash.rb', line 218 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
233 234 235 |
# File 'lib/transproc/hash.rb', line 233 def nest(hash, key, keys) nest!(Hash[hash], key, keys) end |
#nest!(hash, root, keys) ⇒ Object
Same as ‘:nest` but mutates the hash
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/transproc/hash.rb', line 242 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 |
#reject_keys(hash, keys) ⇒ Hash
Rejects specified keys from a hash
169 170 171 |
# File 'lib/transproc/hash.rb', line 169 def reject_keys(hash, keys) reject_keys!(Hash[hash], keys) end |
#reject_keys!(hash, keys) ⇒ Object
Same as ‘:reject_keys` but mutates the hash
153 154 155 |
# File 'lib/transproc/hash.rb', line 153 def reject_keys!(hash, keys) hash.reject { |k,_| keys.include?(k) } 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 = nil) ⇒ Hash
Collapse a nested hash from a specified key
264 265 266 267 |
# File 'lib/transproc/hash.rb', line 264 def unwrap(hash, root, keys = nil) 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
274 275 276 277 278 279 280 281 282 |
# File 'lib/transproc/hash.rb', line 274 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 |