Module: Darthjee::CoreExt::Hash::KeyChangeable
- Included in:
- Darthjee::CoreExt::Hash
- Defined in:
- lib/darthjee/core_ext/hash/key_changeable.rb
Overview
Module holding methods responsible for changing / transforming keys of a Hash
Instance Method Summary collapse
-
#append_to_keys(str, options = {}) ⇒ ::Hash
Append a string to all keys.
-
#chain_change_keys(*calls) ⇒ ::Hash
Change all keys without changing the original hash.
-
#chain_change_keys!(*calls) ⇒ ::Hash
Change all keys changing the original hash.
-
#change_keys(options = {}) {|key| ... } ⇒ Object
Change all keys returning the new hash.
-
#change_keys!(options = {}) {|key| ... } ⇒ Object
Change all keys modifying and returning the hash.
-
#prepend_to_keys(str, options = {}) ⇒ ::Hash
prepend a string to all keys.
-
#remap_keys(remap) ⇒ ::Hash
Changes the key of the hash without changing it.
-
#remap_keys!(keys_map) ⇒ ::Hash
Changes the key of the hash changing the original.
-
#sort_keys(options = {}) ⇒ ::Hash
Sorts keys for hash without changing the original.
-
#sort_keys!(options = {}) ⇒ ::Hash
Sorts keys for hash changing the original.
Instance Method Details
#append_to_keys(str, options = {}) ⇒ ::Hash
Append a string to all keys
153 154 155 156 157 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 153 def append_to_keys(str, = {}) change_key_text() do |key| "#{key}#{str}" end end |
#chain_change_keys(*calls) ⇒ ::Hash
Change all keys without changing the original hash
It changes all keysby publically sending methods to the keys
35 36 37 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 35 def chain_change_keys(*calls) deep_dup.chain_change_keys!(*calls) end |
#chain_change_keys!(*calls) ⇒ ::Hash
Change all keys changing the original hash
It changes all keys by publically sending methods to the keys
50 51 52 53 54 55 56 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 50 def chain_change_keys!(*calls) = calls. calls.inject(self) do |h, m| h.change_keys!(, &m) end end |
#change_keys(options = {}) {|key| ... } ⇒ Object
Change all keys returning the new hash
83 84 85 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 83 def change_keys( = {}, &block) deep_dup.change_keys!(, &block) end |
#change_keys!(options = {}) {|key| ... } ⇒ Object
Change all keys modifying and returning the hash
101 102 103 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 101 def change_keys!( = {}, &block) Hash::KeyChanger.new(self).change_keys(, &block) end |
#prepend_to_keys(str, options = {}) ⇒ ::Hash
prepend a string to all keys
129 130 131 132 133 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 129 def prepend_to_keys(str, = {}) change_key_text() do |key| "#{str}#{key}" end end |
#remap_keys(remap) ⇒ ::Hash
Changes the key of the hash without changing it
205 206 207 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 205 def remap_keys(remap) dup.remap_keys!(remap) end |
#remap_keys!(keys_map) ⇒ ::Hash
Changes the key of the hash changing the original
214 215 216 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 214 def remap_keys!(keys_map) KeyChanger.new(self).remap(keys_map) end |
#sort_keys(options = {}) ⇒ ::Hash
Sorts keys for hash without changing the original
191 192 193 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 191 def sort_keys( = {}) Hash::KeysSorter.new(deep_dup, **).sort end |
#sort_keys!(options = {}) ⇒ ::Hash
Sorts keys for hash changing the original
173 174 175 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 173 def sort_keys!( = {}) Hash::KeysSorter.new(self, **).sort end |