Module: Darthjee::CoreExt::Hash::Cameliazable

Included in:
Darthjee::CoreExt::Hash
Defined in:
lib/darthjee/core_ext/hash/cameliazable.rb

Overview

Module holding methods responsible for camelizing keys of a hash

Instance Method Summary collapse

Instance Method Details

#camelize_keys(options = {}) ⇒ ::Hash

Change keys to CamelCase without changing the original hash

Examples:

hash = { first_key: 1, 'second_key' => 2 }
hash.camelize_keys # returns {
                   #   FirstKey: 1,
                   #   'SecondKey' => 2
                   # }
hash = { first_key: 1, 'second_key' => 2 }
options = { uppercase_first_letter: false }
hash.camelize_keys(options) # returns {
                            #   firstKey: 1,
                            #   'secondKey' => 2
                            # }

Parameters:

  • options (::Hash) (defaults to: {})

    options of camelization

Options Hash (options):

  • uppercase_first_letter: (::Boolean)

    flag defining the type of CamelCase

Returns:

  • (::Hash)

    new hash with changed keys

See Also:



36
37
38
# File 'lib/darthjee/core_ext/hash/cameliazable.rb', line 36

def camelize_keys(options = {})
  dup.camelize_keys!(options)
end

#camelize_keys!(options = {}) ⇒ ::Hash

Change keys to CamelCase changing the original hash

Examples:

hash = { first_key: 1, 'second_key' => 2 }
hash.camelize_keys # returns {
                   #   FirstKey: 1,
                   #   'SecondKey' => 2
                   # }
hash = { first_key: 1, 'second_key' => 2 }
options = { uppercase_first_letter: false }
hash.camelize_keys(options) # returns {
                            #   firstKey: 1,
                            #   'secondKey' => 2
                            # }

Parameters:

  • options (::Hash) (defaults to: {})

    options of camelization

Options Hash (options):

  • uppercase_first_letter: (::Boolean)

    flag defining the type of CamelCase

Returns:

  • (::Hash)

    new hash with changed keys

See Also:



51
52
53
# File 'lib/darthjee/core_ext/hash/cameliazable.rb', line 51

def camelize_keys!(options = {})
  Hash::KeyChanger.new(self).camelize_keys(options)
end

#lower_camelize_keys(options = {}) ⇒ ::Hash

Camelize all keys in the hash as ‘key.camelize(:lower)

Examples:

hash = { first_key: 1, 'second_key' => 2 }
hash.lower_camelize_keys # {
                         #   firstKey: 1,
                         #   'secondKey' => 2
                         # }

Returns:

  • (::Hash)

    the resulting hash



66
67
68
# File 'lib/darthjee/core_ext/hash/cameliazable.rb', line 66

def lower_camelize_keys(options = {})
  dup.lower_camelize_keys!(options)
end

#lower_camelize_keys!(options = {}) ⇒ ::Hash

Camelize all keys in the hash

Examples:

hash = { first_key: 1, 'second_key' => 2 }
hash.lower_camelize_keys # {
                         #   firstKey: 1,
                         #   'secondKey' => 2
                         # }

Returns:

  • (::Hash)

    self after changing the keys



75
76
77
78
79
# File 'lib/darthjee/core_ext/hash/cameliazable.rb', line 75

def lower_camelize_keys!(options = {})
  options = options.merge(uppercase_first_letter: false)

  camelize_keys!(options)
end

#underscore_keys(options = {}) ⇒ Object



81
82
83
# File 'lib/darthjee/core_ext/hash/cameliazable.rb', line 81

def underscore_keys(options = {})
  dup.underscore_keys!(options)
end

#underscore_keys!(options = {}) ⇒ Object



85
86
87
# File 'lib/darthjee/core_ext/hash/cameliazable.rb', line 85

def underscore_keys!(options = {})
  Hash::KeyChanger.new(self).underscore_keys(options)
end