Class: Darthjee::CoreExt::Hash::KeyChanger Private
- Defined in:
- lib/darthjee/core_ext/hash/key_changer.rb
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(options = {}) ⇒ ::Hash
private
Performs camelization of the keys of the hash.
-
#change_keys(options = {}, &block) ⇒ Object
private
Change the keys of the given hash returning the new hash.
- #change_text(options = {}) ⇒ Object private
-
#initialize(hash) ⇒ KeyChanger
constructor
private
A new instance of KeyChanger.
- #remap(keys_map) ⇒ Object private
- #underscore_keys(options = {}) ⇒ Object private
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.
8 9 10 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 8 def initialize(hash) @hash = hash end |
Instance Method Details
#camelize_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.
Performs camelization of the keys of the hash
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 50 def camelize_keys( = {}) = { uppercase_first_letter: true }.merge!() type = [:uppercase_first_letter] ? :upper : :lower change_keys() do |k| k.camelize(type) end end |
#change_keys(options = {}, &block) ⇒ Object
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
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 30 def change_keys( = {}, &block) = { recursive: true }.merge!() if [:recursive] hash.deep_transform_keys!(&block) else hash.transform_keys!(&block) end end |
#change_text(options = {}) ⇒ Object
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.
66 67 68 69 70 71 72 73 74 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 66 def change_text( = {}) = { type: :keep }.merge!() change_keys() do |key| cast_new_key yield(key), key.class, end end |
#remap(keys_map) ⇒ Object
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.
12 13 14 15 16 17 18 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 12 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 = {}) ⇒ Object
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.
62 63 64 |
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 62 def underscore_keys( = {}) change_keys(, &:underscore) end |