Class: Darthjee::CoreExt::Hash::KeyChanger Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Author:

  • Darthjee

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ KeyChanger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of KeyChanger.



10
11
12
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 10

def initialize(hash)
  @hash = hash
end

Instance Method Details

#camelize_keys(uppercase_first_letter: true, **options) ⇒ ::Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Performs camelization of the keys of the hash

Examples:

hash = { my_key: { inner_key: 10 } }
changer = Darthjee::CoreExt::Hash::KeyChanger.new(hash)
changer.camelize_keys
hash   # changed to { MyKey: { InnerKey: 10 } }
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)
  • uppercase_first_letter (::TrueClass, ::FalseClass) (defaults to: true)

    flag defining the type of CamelCase

Options Hash (**options):

  • recursive (::TrueClass, ::FalseClass) — default: true

    flag defining the change to happen also on inner hashes

Returns:

  • (::Hash)

    the given hash with it’s keys changed

See Also:



95
96
97
98
99
100
101
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 95

def camelize_keys(uppercase_first_letter: true, **options)
  type = uppercase_first_letter ? :upper : :lower

  change_keys(options) do |key|
    key.camelize(type)
  end
end

#change_keys(recursive: true, &block) ⇒ ::Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Change the keys of the given hash returning the new hash

Examples:

hash = { a: 1, 'b' => { c: 3 } }
changer = Darthjee::CoreExt::Hash::KeyChanger.new(hash)
changer.change_keys { |k| "key_#{k}" }

hash # changed to {
     #   'key_a' => 1,
     #   'key_b' => {
     #     'key_c' => 3
     #   }
     # }
hash = { '1' => 1, '2' => { '3' => 2} }

result = hash.change_keys do |k|
  (k.to_i + 1).to_s.to_sym
end
result   # returns { :'2' => 1, :'3' => { :'4' => 2 } }

result = hash.change_keys(recursive:false) do |k|
  (k.to_i + 1).to_s.to_sym
end
result    # returns { :'2' => 1, :'3' => { '3' => 2 } }

Parameters:

  • recursive (::TrueClass, ::FalseClass) (defaults to: true)

    flag defining the change to happen also on inner hashes

Returns:

  • (::Hash)

    Given hash after keys tranformation



64
65
66
67
68
69
70
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 64

def change_keys(recursive: true, &block)
  if recursive
    hash.deep_transform_keys!(&block)
  else
    hash.transform_keys!(&block)
  end
end

#change_text(type: :keep, **options) {|key| ... } ⇒ ::Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Change keys considering them to be strings

Examples:

hash = { key: { inner_key: 10 } }
changer = Darthjee::CoreExt::Hash::KeyChanger.new(hash)
changer.change_text { |key| key.to_s.upcase }

hash  # changed to { KEY: { INNER_KEY: 10 } }

Parameters:

  • options (::Hash)
  • type (::Symbol) (defaults to: :keep)

    type that key will be case

    • keep: Cast the key back to the same type it was

    • string cast the key to String

    • symbol cast the key to Symbol

Options Hash (**options):

  • recursive (::TrueClass, ::FalseClass) — default: true

    flag defining the change to happen also on inner hashes

Yields:

  • (key)

    key transformation block

Returns:

  • (::Hash)

    the given hash with changed keys



146
147
148
149
150
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 146

def change_text(type: :keep, **options)
  change_keys(**options) do |key|
    cast_new_key yield(key), key.class, type
  end
end

#remap(keys_map) ⇒ ::Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Changes keys based on map

Examples:

hash = { a: 1, 'b' => 2 }
changer = Darthjee::CoreExt::Hash::KeyChanger.new(hash)
remap_map = { a: 1, 'b' => 2 }

changer.remap(remap_map)

hash   # changed to {
       #   za: 1,
       #   'yb' => 2,
       #   zb: nil
       # }
hash = { a: 1, b: 2 }
hash.remap_keys(a: :b, b: :c) # returns {
                              #   b: 1,
                              #   c: 2
                              # }

Parameters:

  • keys_map (::Hash)

    map of original => final key

Returns:

  • (::Hash)

    the given hash modified



35
36
37
38
39
40
41
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 35

def remap(keys_map)
  new_hash = {}
  keys_map.each do |o, n|
    new_hash[n] = hash.delete(o)
  end
  hash.merge! new_hash
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Changes keys by performing underscore transformation

Examples:

hash = { myKey: { InnerKey: 10 } }
changer = Darthjee::CoreExt::Hash::KeyChanger.new(hash)
changer.underscore_keys

hash  # changed to { my_key: { inner_key: 10 } }

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) — default: true

    flag defining the change to happen also on inner hashes

Returns:



120
121
122
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 120

def underscore_keys(options = {})
  change_keys(options, &:underscore)
end