Class: Darthjee::CoreExt::Hash::KeyChanger Private
- Defined in:
- lib/darthjee/core_ext/hash/key_changer.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Instance Method Summary collapse
-
#camelize_keys(uppercase_first_letter: true, **options) ⇒ ::Hash
private
Performs camelization of the keys of the hash.
-
#change_keys(recursive: true, &block) ⇒ ::Hash
private
Change the keys of the given hash returning the new hash.
-
#change_text(type: :keep, **options) {|key| ... } ⇒ ::Hash
private
Change keys considering them to be strings.
-
#initialize(hash) ⇒ KeyChanger
constructor
private
A new instance of KeyChanger.
-
#remap(keys_map) ⇒ ::Hash
private
Changes keys based on map.
-
#underscore_keys(options = {}) ⇒ ::Hash
private
Changes keys by performing underscore transformation.
Constructor Details
#initialize(hash) ⇒ KeyChanger
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of KeyChanger.
10 11 12 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 10 def initialize(hash) @hash = hash end |
Instance Method Details
#camelize_keys(uppercase_first_letter: true, **options) ⇒ ::Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Performs camelization of the keys of the hash
95 96 97 98 99 100 101 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 95 def camelize_keys(uppercase_first_letter: true, **) type = uppercase_first_letter ? :upper : :lower change_keys() do |key| key.camelize(type) end end |
#change_keys(recursive: true, &block) ⇒ ::Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Change the keys of the given hash returning the new hash
64 65 66 67 68 69 70 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 64 def change_keys(recursive: true, &block) if recursive hash.deep_transform_keys!(&block) else hash.transform_keys!(&block) end end |
#change_text(type: :keep, **options) {|key| ... } ⇒ ::Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Change keys considering them to be strings
146 147 148 149 150 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 146 def change_text(type: :keep, **) change_keys(**) do |key| cast_new_key yield(key), key.class, type end end |
#remap(keys_map) ⇒ ::Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Changes keys based on map
35 36 37 38 39 40 41 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 35 def remap(keys_map) new_hash = {} keys_map.each do |o, n| new_hash[n] = hash.delete(o) end hash.merge! new_hash end |
#underscore_keys(options = {}) ⇒ ::Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Changes keys by performing underscore transformation
120 121 122 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 120 def underscore_keys( = {}) change_keys(, &:underscore) end |