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

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

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.

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.



8
9
10
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 8

def initialize(hash)
  @hash = hash
end

Instance Method Details

#camelize_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.

Performs camelization of the keys of the hash

Parameters:

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

    options

Options Hash (options):

  • uppercase_first_letter: (::Boolean)

    flag defining the type of CamelCase

Returns:

  • (::Hash)

    the given hash with it’s keys changed



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 50

def camelize_keys(options = {})
  options = {
    uppercase_first_letter: true
  }.merge!(options)

  type = options[:uppercase_first_letter] ? :upper : :lower

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

#change_keys(options = {}, &block) ⇒ Object

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

Parameters:

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

    options for transformation

Options Hash (options):

  • recursive: (Boolean)

    flag defining the change to happen also on inner hashes (defaults to: true)

Returns:

  • New hash after keys tranformation



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 30

def change_keys(options = {}, &block)
  options = {
    recursive: true
  }.merge!(options)

  if options[:recursive]
    hash.deep_transform_keys!(&block)
  else
    hash.transform_keys!(&block)
  end
end

#change_text(options = {}) ⇒ Object

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.



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

def change_text(options = {})
  options = {
    type: :keep
  }.merge!(options)

  change_keys(options) do |key|
    cast_new_key yield(key), key.class, options
  end
end

#remap(keys_map) ⇒ Object

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.



12
13
14
15
16
17
18
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 12

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 = {}) ⇒ Object

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.



62
63
64
# File 'lib/darthjee/core_ext/hash/key_changer.rb', line 62

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