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) ⇒ ::Hash
private
Performs camelization of the keys of the hash.
-
#change_keys(recursive: true) ⇒ ::Hash
private
Change the keys of the given hash returning the new hash.
-
#change_text(type: :keep) {|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 ⇒ ::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.
11 12 13 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 11 def initialize(hash) @hash = hash end |
Instance Method Details
#camelize_keys(uppercase_first_letter: true) ⇒ ::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
96 97 98 99 100 101 102 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 96 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) ⇒ ::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
65 66 67 68 69 70 71 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 65 def change_keys(recursive: true, &) if recursive hash.deep_transform_keys!(&) else hash.transform_keys!(&) end end |
#change_text(type: :keep) {|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
147 148 149 150 151 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 147 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
36 37 38 39 40 41 42 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 36 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 ⇒ ::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
121 122 123 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 121 def underscore_keys(**) change_keys(**, &:underscore) end |