Module: Darthjee::CoreExt::Hash::KeyChangeable
- Included in:
- Darthjee::CoreExt::Hash
- Defined in:
- lib/darthjee/core_ext/hash/key_changeable.rb
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 }.
- #camelize_keys(options = {}) ⇒ Object
- #camelize_keys!(options = {}) ⇒ Object
-
#chain_change_keys(*calls) ⇒ Object
change all publicaly sending method calls options: { recursive: true } ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { “A” =>1 }.
-
#chain_change_keys!(*calls) ⇒ Object
change all publicaly sending method calls options: { recursive: true } ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { “A” =>1 }.
-
#change_keys(options = {}, &block) ⇒ Object
change all keys returning the new map options: { recursive: true } ex: { “a” =>1 }.change_keys{ |key| key.upcase } == { “A” => 1 }.
-
#change_keys!(options = {}, &block) ⇒ Object
change all keys returning the new map options: { recursive: true } ex: { “a”:1 }.change_keys{ |key| key.upcase } == { “A”:1 }.
-
#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
- #lower_camelize_keys(options = {}) ⇒ Object
- #lower_camelize_keys!(options = {}) ⇒ 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) ⇒ Object
- #remap_keys!(keys_map) ⇒ Object
-
#sort_keys(options = {}) ⇒ Object
sorts keys for hash options: { recursive: true } ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }.
- #underscore_keys(options = {}) ⇒ Object
- #underscore_keys!(options = {}) ⇒ Object
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 }
93 94 95 96 97 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 93 def append_to_keys(str, = {}) change_key_text() do |key| "#{key}#{str}" end end |
#camelize_keys(options = {}) ⇒ Object
25 26 27 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 25 def camelize_keys( = {}) dup.camelize_keys!() end |
#camelize_keys!(options = {}) ⇒ Object
29 30 31 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 29 def camelize_keys!( = {}) Hash::KeyChanger.new(self).camelize_keys() end |
#chain_change_keys(*calls) ⇒ Object
change all publicaly sending method calls options: { recursive: true } ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { “A” =>1 }
58 59 60 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 58 def chain_change_keys(*calls) deep_dup.chain_change_keys!(*calls) end |
#chain_change_keys!(*calls) ⇒ Object
change all publicaly sending method calls options: { recursive: true } ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { “A” =>1 }
65 66 67 68 69 70 71 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 65 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 map options: { recursive: true } ex: { “a” =>1 }.change_keys{ |key| key.upcase } == { “A” => 1 }
44 45 46 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 44 def change_keys( = {}, &block) deep_dup.change_keys!(, &block) end |
#change_keys!(options = {}, &block) ⇒ Object
change all keys returning the new map options: { recursive: true } ex: { “a”:1 }.change_keys{ |key| key.upcase } == { “A”:1 }
51 52 53 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 51 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“ } }
120 121 122 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 120 def change_values( = {}, &block) deep_dup.change_values!(, &block) end |
#change_values!(options = {}, &block) ⇒ Object
124 125 126 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 124 def change_values!( = {}, &block) Hash::ValueChanger.new(, &block).change(self) end |
#lower_camelize_keys(options = {}) ⇒ Object
15 16 17 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 15 def lower_camelize_keys( = {}) dup.lower_camelize_keys!() end |
#lower_camelize_keys!(options = {}) ⇒ Object
19 20 21 22 23 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 19 def lower_camelize_keys!( = {}) = .merge(uppercase_first_letter: false) camelize_keys!() 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 }
80 81 82 83 84 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 80 def prepend_to_keys(str, = {}) change_key_text() do |key| "#{str}#{key}" end end |
#remap_keys(remap) ⇒ Object
7 8 9 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 7 def remap_keys(remap) dup.remap_keys!(remap) end |
#remap_keys!(keys_map) ⇒ Object
11 12 13 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 11 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 }
102 103 104 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 102 def sort_keys( = {}) Hash::KeysSorter.new(self, **).sort end |
#underscore_keys(options = {}) ⇒ Object
33 34 35 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 33 def underscore_keys( = {}) dup.underscore_keys!() end |
#underscore_keys!(options = {}) ⇒ Object
37 38 39 |
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 37 def underscore_keys!( = {}) Hash::KeyChanger.new(self).underscore_keys() end |