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

Instance Method Summary collapse

Instance Method Details

#append_to_keys(str, options = {}) ⇒ Object

append a string to all keys options

recursive: true,
type: :keep [keep, string, symbol] (key type to be returned)

ex: { :a => 1, “b”=> 2 }.append_to_keys(“_bar”) # returns { :a_bar => 1, “b_bar”=> 2 }



105
106
107
108
109
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 105

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

#chain_change_keys(*calls) ⇒ ::Hash

Change all keys by publically sending methods to the keys without changing the original hash

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:



23
24
25
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 23

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

#chain_change_keys!(*calls) ⇒ ::Hash

Change all keys by publically sending methods to the keys changing the original hash

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:



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

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 = {}, &block) ⇒ 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: (Boolean)

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

Returns:

  • new Hash with modified keys

See Also:



66
67
68
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 66

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

#change_keys!(options = {}, &block) ⇒ 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: (Boolean)

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

Returns:

  • self

See Also:



81
82
83
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 81

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

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

creates a new hash with changes in its values options:

recursive: true,
skip_hash:true

ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 } ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s } # returns { a:“1”, b:“{ c=>1 } ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s } # returns { a:”1“, b:{ c=>”1“ } }



132
133
134
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 132

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

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



136
137
138
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 136

def change_values!(options = {}, &block)
  Hash::ValueChanger.new(options, &block).change(self)
end

#prepend_to_keys(str, options = {}) ⇒ Object

prepend a string to all keys options

recursive: true,
type: :keep [keep, string, symbol] (key type to be returned)

ex: { :a => 1, “b”=> 2 }.prepend_to_keys(“foo_”) # returns { :foo_a => 1, “foo_b”=> 2 }



92
93
94
95
96
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 92

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:



147
148
149
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 147

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:



156
157
158
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 156

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

#sort_keys(options = {}) ⇒ Object

sorts keys for hash options: { recursive: true } ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }



114
115
116
# File 'lib/darthjee/core_ext/hash/key_changeable.rb', line 114

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