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

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

Overview

Module holding methods 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: (::TrueClass, ::FalseClass)

    flag defining the type of CamelCase

Returns:

  • (::Hash)

    new hash with changed keys

See Also:



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

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: (::TrueClass, ::FalseClass)

    flag defining the type of CamelCase

Returns:

  • (::Hash)

    new hash with changed keys

See Also:



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

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



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

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



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

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

  camelize_keys!(options)
end

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

Change all keys to be snakecase

THis method does not change the original hash

Examples:

underscoring all keys

hash = { firstKey: 1, 'SecondKey' => 2 }

hash.underscore_keys  # returns {
                      #   first_key: 1,
                      #   'second_key' => 2
                      # }

Parameters:

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

Options Hash (options):

  • recursive (::TrueClass, ::FalseClass)

    flag for recursive transformation

Returns:

See Also:



99
100
101
# File 'lib/darthjee/core_ext/hash/cameliazable.rb', line 99

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

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

Change all keys to be snakecase

THis method changes the original hash

Examples:

underscoring all keys

hash = { firstKey: 1, 'SecondKey' => 2 }

hash.underscore_keys  # returns {
                      #   first_key: 1,
                      #   'second_key' => 2
                      # }

Parameters:

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

Options Hash (options):

  • recursive (::TrueClass, ::FalseClass)

    flag for recursive transformation

Returns:

See Also:



116
117
118
# File 'lib/darthjee/core_ext/hash/cameliazable.rb', line 116

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