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::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)

    options of camelization

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(**)
  dup.camelize_keys!(**)
end

#camelize_keys!::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)

    options of camelization

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!(**)
  Hash::KeyChanger.new(self).camelize_keys(**)
end

#lower_camelize_keys::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(**)
  dup.lower_camelize_keys!(**)
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::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:

Returns:

See Also:



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

def underscore_keys(**)
  dup.underscore_keys!(**)
end

#underscore_keys!::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:

Returns:

See Also:



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

def underscore_keys!(**)
  Hash::KeyChanger.new(self).underscore_keys(**)
end