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 = {}) ⇒ Object
append a string to all keys options { recursive: true, type: :keep [keep, string, symbol] (key type to be returned) } ex: { :a => 1, “b”=> 2 }.append_to_keys(“_bar”) # returns { :a_bar => 1, “b_bar”=> 2 }.
-
#chain_change_keys(*calls) ⇒ ::Hash
Change all keys by publically sending methods to the keys without changing the original hash.
-
#chain_change_keys!(*calls) ⇒ ::Hash
Change all keys by publically sending methods to the keys changing the original hash.
-
#change_keys(options = {}, &block) ⇒ Object
Change all keys returning the new hash.
-
#change_keys!(options = {}, &block) ⇒ Object
Change all keys modifying and returning the hash.
-
#change_values(options = {}, &block) ⇒ Object
creates a new hash with changes in its values options: { recursive: true, skip_hash:true } ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 } ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s } # returns { a:“1”, b:“{ c=>1 } ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s } # returns { a:”1“, b:{ c=>”1“ } }.
- #change_values!(options = {}, &block) ⇒ Object
-
#prepend_to_keys(str, options = {}) ⇒ Object
prepend a string to all keys options { recursive: true, type: :keep [keep, string, symbol] (key type to be returned) } ex: { :a => 1, “b”=> 2 }.prepend_to_keys(“foo_”) # returns { :foo_a => 1, “foo_b”=> 2 }.
-
#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 = {}) ⇒ Object
sorts keys for hash options: { recursive: true } ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }.
Instance Method Details
#append_to_keys(str, options = {}) ⇒ Object
append a string to all keys options
recursive: true,
type: :keep [keep, string, symbol] (key type to be returned)
ex: { :a => 1, “b”=> 2 }.append_to_keys(“_bar”) # returns { :a_bar => 1, “b_bar”=> 2 }
105 106 107 108 109 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 105 def append_to_keys(str, = {}) change_key_text() do |key| "#{key}#{str}" end end |
#chain_change_keys(*calls) ⇒ ::Hash
Change all keys by publically sending methods to the keys without changing the original hash
23 24 25 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 23 def chain_change_keys(*calls) deep_dup.chain_change_keys!(*calls) end |
#chain_change_keys!(*calls) ⇒ ::Hash
Change all keys by publically sending methods to the keys changing the original hash
36 37 38 39 40 41 42 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 36 def chain_change_keys!(*calls) = calls. calls.inject(self) do |h, m| h.change_keys!(, &m) end end |
#change_keys(options = {}, &block) ⇒ Object
Change all keys returning the new hash
66 67 68 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 66 def change_keys( = {}, &block) deep_dup.change_keys!(, &block) end |
#change_keys!(options = {}, &block) ⇒ Object
Change all keys modifying and returning the hash
81 82 83 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 81 def change_keys!( = {}, &block) Hash::KeyChanger.new(self).change_keys(, &block) end |
#change_values(options = {}, &block) ⇒ Object
creates a new hash with changes in its values options:
recursive: true,
skip_hash:true
ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 } ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s } # returns { a:“1”, b:“{ c=>1 } ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s } # returns { a:”1“, b:{ c=>”1“ } }
132 133 134 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 132 def change_values( = {}, &block) deep_dup.change_values!(, &block) end |
#change_values!(options = {}, &block) ⇒ Object
136 137 138 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 136 def change_values!( = {}, &block) Hash::ValueChanger.new(, &block).change(self) end |
#prepend_to_keys(str, options = {}) ⇒ Object
prepend a string to all keys options
recursive: true,
type: :keep [keep, string, symbol] (key type to be returned)
ex: { :a => 1, “b”=> 2 }.prepend_to_keys(“foo_”) # returns { :foo_a => 1, “foo_b”=> 2 }
92 93 94 95 96 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 92 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
147 148 149 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 147 def remap_keys(remap) dup.remap_keys!(remap) end |
#remap_keys!(keys_map) ⇒ ::Hash
Changes the key of the hash changing the original
156 157 158 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 156 def remap_keys!(keys_map) KeyChanger.new(self).remap(keys_map) end |
#sort_keys(options = {}) ⇒ Object
sorts keys for hash options: { recursive: true } ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }
114 115 116 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 114 def sort_keys( = {}) Hash::KeysSorter.new(self, **).sort end |