Class: Hash::KeyChanger
- Inherits:
-
Object
- Object
- Hash::KeyChanger
- Defined in:
- lib/hash/key_changer.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #camelize_keys(settings = {}) ⇒ Object
- #change_keys(settings = {}, &block) ⇒ Object
- #change_text(options = {}, &block) ⇒ Object
-
#initialize(hash) ⇒ KeyChanger
constructor
A new instance of KeyChanger.
- #underscore_keys(settings = {}) ⇒ Object
Constructor Details
#initialize(hash) ⇒ KeyChanger
Returns a new instance of KeyChanger.
4 5 6 |
# File 'lib/hash/key_changer.rb', line 4 def initialize(hash) @hash = hash end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
2 3 4 |
# File 'lib/hash/key_changer.rb', line 2 def block @block end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
2 3 4 |
# File 'lib/hash/key_changer.rb', line 2 def hash @hash end |
Instance Method Details
#camelize_keys(settings = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hash/key_changer.rb', line 20 def camelize_keys(settings = {}) ({ uppercase_first_letter: true }, settings) type = [:uppercase_first_letter] ? :upper : :lower change_keys do |k| k.camelize(type) end end |
#change_keys(settings = {}, &block) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/hash/key_changer.rb', line 8 def change_keys(settings = {}, &block) ({ recursive: true }, settings) if [:recursive] hash.deep_transform_keys!(&block) else hash.transform_keys!(&block) end end |
#change_text(options = {}, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/hash/key_changer.rb', line 40 def change_text( = {}, &block) ({ type: :keep }, ) change_keys do |key| cast_new_key block.call(key), key.class end end |
#underscore_keys(settings = {}) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/hash/key_changer.rb', line 32 def underscore_keys(settings = {}) ({}, settings) change_keys do |k| k.underscore end end |