Module: Darthjee::CoreExt::Hash::KeyChangeable

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

Overview

Module holding methods responsible for changing / transforming keys of a Hash

See Also:

Author:

  • Darthjee

Instance Method Summary collapse

Instance Method Details

#append_to_keys(str, options = {}) ⇒ ::Hash

Append a string to all keys

Examples:

hash = { :a => 1, "b"=> 2 }

hash.prepend_to_keys("foo_") # returns {
                             #   :foo_a => 1,
                             #   "foo_b"=> 2
                             # }

Parameters:

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

Options Hash (options):

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

    flag indicating transformation should be recursive

  • type (::Symbol) — default: :keep

    type of the final key

    • keep : cast the result to the same type of the original key

    • string : cast the result to be String

    • symbol cast the result to be Symbol

Returns:

See Also:



153
154
155
156
157
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 153

def append_to_keys(str, options = {})
  change_key_text(options) do |key|
    "#{key}#{str}"
  end
end

#chain_change_keys(*calls) ⇒ ::Hash

Change all keys without changing the original hash

It changes all keysby publically sending methods to the keys

Examples:

hash = { first: 1, second: 2 }
resut = hash.chain_change_keys(:to_s, :size, :to_s, :to_sym)
result     # returns { :'5' => 1, :'6' => 2 }

Parameters:

Returns:

  • (::Hash)

    New hash with the resulting keys

See Also:



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

def chain_change_keys(*calls)
  deep_dup.chain_change_keys!(*calls)
end

#chain_change_keys!(*calls) ⇒ ::Hash

Change all keys changing the original hash

It changes all keys by publically sending methods to the keys

Examples:

hash = { first: 1, second: 2 }
resut = hash.chain_change_keys(:to_s, :size, :to_s, :to_sym)
result     # returns { :'5' => 1, :'6' => 2 }

Parameters:

Returns:

  • (::Hash)

    New hash with the resulting keys

See Also:



50
51
52
53
54
55
56
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 50

def chain_change_keys!(*calls)
  options = calls.extract_options!

  calls.inject(self) do |h, m|
    h.change_keys!(options, &m)
  end
end

#change_keys(options = {}) {|key| ... } ⇒ Object

Change all keys returning the new hash

Examples:

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:

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

    options to passed to KeyChanger

Options Hash (options):

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

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

Yields:

  • (key)

    changing key block

Returns:

  • new Hash with modified keys

See Also:



83
84
85
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 83

def change_keys(options = {}, &block)
  deep_dup.change_keys!(options, &block)
end

#change_keys!(options = {}) {|key| ... } ⇒ Object

Change all keys modifying and returning the hash

Examples:

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:

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

    options to passed to KeyChanger

Options Hash (options):

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

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

Yields:

  • (key)

    changing key block

Returns:

  • self

See Also:



101
102
103
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 101

def change_keys!(options = {}, &block)
  Hash::KeyChanger.new(self).change_keys(options, &block)
end

#prepend_to_keys(str, options = {}) ⇒ ::Hash

prepend a string to all keys

Examples:

hash = { :a => 1, "b"=> 2 }

hash.prepend_to_keys("foo_") # returns {
                             #   :foo_a => 1,
                             #   "foo_b"=> 2
                             # }

Parameters:

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

Options Hash (options):

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

    flag indicating transformation should be recursive

  • type (::Symbol) — default: :keep

    type of the final key

    • keep : cast the result to the same type of the original key

    • string : cast the result to be String

    • symbol cast the result to be Symbol

Returns:

See Also:



129
130
131
132
133
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 129

def prepend_to_keys(str, options = {})
  change_key_text(options) do |key|
    "#{str}#{key}"
  end
end

#remap_keys(remap) ⇒ ::Hash

Changes the key of the hash without changing it

Examples:

hash = { a: 1, b: 2 }
hash.remap_keys(a: :b, b: :c) # returns {
                              #   b: 1,
                              #   c: 2
                              # }

Returns:



205
206
207
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 205

def remap_keys(remap)
  dup.remap_keys!(remap)
end

#remap_keys!(keys_map) ⇒ ::Hash

Changes the key of the hash changing the original

Examples:

hash = { a: 1, b: 2 }
hash.remap_keys(a: :b, b: :c) # returns {
                              #   b: 1,
                              #   c: 2
                              # }

Returns:



214
215
216
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 214

def remap_keys!(keys_map)
  KeyChanger.new(self).remap(keys_map)
end

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

Sorts keys for hash without changing the original

Examples:

hash = { b: 1, a: 2 }

hash.sort_keys  # returns { a: 2, b: 1 }

Parameters:

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

Options Hash (options):

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

    flag indicating recursive sorting

Returns:

See Also:

  • KeySorter#sort


191
192
193
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 191

def sort_keys(options = {})
  Hash::KeysSorter.new(deep_dup, **options).sort
end

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

Sorts keys for hash changing the original

Examples:

hash = { b: 1, a: 2 }

hash.sort_keys  # changes hash to { a: 2, b: 1 }

Parameters:

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

Options Hash (options):

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

    flag indicating recursive sorting

Returns:

See Also:

  • KeySorter#sort


173
174
175
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 173

def sort_keys!(options = {})
  Hash::KeysSorter.new(self, **options).sort
end