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.

Parameters:

  • hash (::Hash)

    Hash to change keys of



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

def initialize(hash)
  @hash = hash
end

Instance Method Details

#camelize_keys(uppercase_first_letter: true) ⇒ ::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

Returns:

  • (::Hash)

    the given hash with it's keys changed

See Also:



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

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

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

#change_keys(recursive: true) ⇒ ::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



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

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

#change_text(type: :keep) {|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

Yields:

  • (key)

    key transformation block

Returns:

  • (::Hash)

    the given hash with changed keys



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

def change_text(type: :keep, **)
  change_keys(**) 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



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

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

Returns:



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

def underscore_keys(**)
  change_keys(**, &:underscore)
end