Module: Darthjee::CoreExt::Hash::Transformable
- Included in:
- Darthjee::CoreExt::Hash
- Defined in:
- lib/darthjee/core_ext/hash/transformable.rb
Instance Method Summary collapse
-
#exclusive_merge(other) ⇒ ::Hash
Merge only common keys.
-
#exclusive_merge!(other) ⇒ ::Hash
Merge only common keys.
-
#map_to_hash {|key, value| ... } ⇒ ::Hash
Map returning a hash keeping the original keys.
-
#squash(joiner = '.') ⇒ ::Hash
Squash the hash returning a single level hash.
-
#squash!(joiner = '.') ⇒ ::Hash
Squash the hash so that it becomes a single level hash.
-
#to_deep_hash(separator = '.') ⇒ ::Hash
Creates a new hash of multiple levels.
-
#to_deep_hash!(separator = '.') ⇒ ::Hash
Changes hash to be a multiple level hash.
Instance Method Details
#exclusive_merge(other) ⇒ ::Hash
Merge only common keys
22 23 24 |
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 22 def exclusive_merge(other) dup.exclusive_merge!(other) end |
#exclusive_merge!(other) ⇒ ::Hash
Merge only common keys
41 42 43 |
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 41 def exclusive_merge!(other) merge!(other.slice(*keys)) end |
#map_to_hash {|key, value| ... } ⇒ ::Hash
Map returning a hash keeping the original keys
Run map block where each pair key, value is mapped to a new value to be assigned in the same key on the returned hash
70 71 72 73 74 |
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 70 def map_to_hash map do |key, value| [key, yield(key, value)] end.to_h end |
#squash(joiner = '.') ⇒ ::Hash
Squash the hash returning a single level hash
The squashing happens by merging the keys of outter and inner hashes
This operation is the oposite of #to_deep_hash
133 134 135 |
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 133 def squash(joiner = '.') Hash::Squasher.new(joiner).squash(deep_dup) end |
#squash!(joiner = '.') ⇒ ::Hash
Squash the hash so that it becomes a single level hash
The squashing happens by merging the keys of outter and inner hashes
This operation is the oposite of #to_deep_hash!
153 154 155 |
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 153 def squash!(joiner = '.') Hash::Squasher.new(joiner).squash(self) end |