Class: Hash::KeyChanger

Inherits:
Object
  • Object
show all
Defined in:
lib/hash/key_changer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#blockObject (readonly)

Returns the value of attribute block.



2
3
4
# File 'lib/hash/key_changer.rb', line 2

def block
  @block
end

#hashObject (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 = {})
  merge_options({
    uppercase_first_letter: true
  }, settings)

  type = options[: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)
  merge_options({
    recursive: true
  }, settings)

  if options[: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(options = {}, &block)
  merge_options({
    type: :keep
  }, options)

  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 = {})
  merge_options({}, settings)

  change_keys do |k|
    k.underscore
  end
end