Module: Transproc::HashTransformations

Extended by:
Functions
Defined in:
lib/transproc/hash.rb

Overview

Transformation functions for Hash objects

Examples:

require 'transproc/hash'

include Transproc::Helper

fn = t(:symbolize_keys) >> t(:nest, :address, [:street, :zipcode])

fn["street" => "Street 1", "zipcode" => "123"]
# => {:address => {:street => "Street 1", :zipcode => "123"}}

Instance Method Summary collapse

Methods included from Functions

method_added

Instance Method Details

#accept_keys(hash, keys) ⇒ Hash

Accepts specified keys from a hash

Examples:

Transproc(:accept_keys, [:name])[name: 'Jane', email: '[email protected]']
# => {:email => "[email protected]"}

Parameters:

  • hash (Hash)

    The input hash

  • keys (Array)

    The keys to be accepted

Returns:

  • (Hash)


194
195
196
# File 'lib/transproc/hash.rb', line 194

def accept_keys(hash, keys)
  accept_keys!(Hash[hash], keys)
end

#accept_keys!(hash, keys) ⇒ Object

Same as ‘:accept_keys` but mutates the hash

See Also:



178
179
180
# File 'lib/transproc/hash.rb', line 178

def accept_keys!(hash, keys)
  reject_keys!(hash, hash.keys - keys)
end

#map_keys(hash, fn) ⇒ Hash

Map all keys in a hash with the provided transformation function

Examples:

Transproc(:map_keys, -> s { s.upcase })['name' => 'Jane']
# => {"NAME" => "Jane"}

Parameters:

  • (Hash)

Returns:

  • (Hash)


31
32
33
# File 'lib/transproc/hash.rb', line 31

def map_keys(hash, fn)
  map_keys!(Hash[hash], fn)
end

#map_keys!(hash, fn) ⇒ Object

Same as ‘:map_keys` but mutates the hash

See Also:



40
41
42
43
# File 'lib/transproc/hash.rb', line 40

def map_keys!(hash, fn)
  hash.keys.each { |key| hash[fn[key]] = hash.delete(key) }
  hash
end

#map_value(hash, key, fn) ⇒ Hash

Map a key in a hash with the provided transformation function

Examples:

Transproc(:map_value, 'name', -> s { s.upcase })['name' => 'jane']
# => {"name" => "JANE"}

Parameters:

  • (Hash)

Returns:

  • (Hash)


209
210
211
# File 'lib/transproc/hash.rb', line 209

def map_value(hash, key, fn)
  hash.merge(key => fn[hash[key]])
end

#map_value!(hash, key, fn) ⇒ Object

Same as ‘:map_value` but mutates the hash

See Also:



218
219
220
# File 'lib/transproc/hash.rb', line 218

def map_value!(hash, key, fn)
  hash.update(key => fn[hash[key]])
end

#map_values(hash, fn) ⇒ Hash

Map all values in a hash using transformation function

Examples:

Transproc(:map_values, -> v { v.upcase })[:name => 'Jane']
# => {"name" => "JANE"}

Parameters:

  • (Hash)

Returns:

  • (Hash)


104
105
106
# File 'lib/transproc/hash.rb', line 104

def map_values(hash, fn)
  map_values!(Hash[hash], fn)
end

#map_values!(hash, fn) ⇒ Hash

Same as ‘:map_values` but mutates the hash

Parameters:

  • (Hash)

Returns:

  • (Hash)

See Also:



117
118
119
120
# File 'lib/transproc/hash.rb', line 117

def map_values!(hash, fn)
  hash.each { |key, value| hash[key] = fn[value] }
  hash
end

#nest(hash, key, keys) ⇒ Hash

Nest values from specified keys under a new key

Examples:

Transproc(:nest, :address, [:street, :zipcode])[street: 'Street', zipcode: '123']
# => {address: {street: "Street", zipcode: "123"}}

Parameters:

  • (Hash)

Returns:

  • (Hash)


233
234
235
# File 'lib/transproc/hash.rb', line 233

def nest(hash, key, keys)
  nest!(Hash[hash], key, keys)
end

#nest!(hash, root, keys) ⇒ Object

Same as ‘:nest` but mutates the hash

See Also:



242
243
244
245
246
247
248
249
250
251
# File 'lib/transproc/hash.rb', line 242

def nest!(hash, root, keys)
  nest_keys = hash.keys & keys

  if nest_keys.size > 0
    child = Hash[nest_keys.zip(nest_keys.map { |key| hash.delete(key) })]
    hash.update(root => child)
  else
    hash.update(root => {})
  end
end

#reject_keys(hash, keys) ⇒ Hash

Rejects specified keys from a hash

Examples:

Transproc(:reject_keys, [:name])[name: 'Jane', email: '[email protected]']
# => {:email => "[email protected]"}

Parameters:

  • hash (Hash)

    The input hash

  • keys (Array)

    The keys to be rejected

Returns:

  • (Hash)


169
170
171
# File 'lib/transproc/hash.rb', line 169

def reject_keys(hash, keys)
  reject_keys!(Hash[hash], keys)
end

#reject_keys!(hash, keys) ⇒ Object

Same as ‘:reject_keys` but mutates the hash

See Also:



153
154
155
# File 'lib/transproc/hash.rb', line 153

def reject_keys!(hash, keys)
  hash.reject { |k,_| keys.include?(k) }
end

#rename_keys(hash, mapping) ⇒ Hash

Rename all keys in a hash using provided mapping hash

Examples:

Transproc(:rename_keys, user_name: :name)[user_name: 'Jane']
# => {:name => "Jane"}

Parameters:

  • hash (Hash)

    The input hash

  • mapping (Hash)

    The key-rename mapping

Returns:

  • (Hash)


134
135
136
# File 'lib/transproc/hash.rb', line 134

def rename_keys(hash, mapping)
  rename_keys!(Hash[hash], mapping)
end

#rename_keys!(hash, mapping) ⇒ Object

Same as ‘:rename_keys` but mutates the hash

See Also:



143
144
145
146
# File 'lib/transproc/hash.rb', line 143

def rename_keys!(hash, mapping)
  mapping.each { |k, v| hash[v] = hash.delete(k) }
  hash
end

#stringify_keys(hash) ⇒ Hash

Stringify all keys in a hash

Examples:

Transproc(:stringify_keys)[:name => 'Jane']
# => {"name" => "Jane"}

Parameters:

  • (Hash)

Returns:

  • (Hash)


80
81
82
# File 'lib/transproc/hash.rb', line 80

def stringify_keys(hash)
  stringify_keys!(Hash[hash])
end

#stringify_keys!(hash) ⇒ Object

Same as ‘:stringify_keys` but mutates the hash

See Also:



89
90
91
# File 'lib/transproc/hash.rb', line 89

def stringify_keys!(hash)
  map_keys!(hash, Transproc(:to_string).fn)
end

#symbolize_keys(hash) ⇒ Hash

Symbolize all keys in a hash

Examples:

Transproc(:symbolize_keys)['name' => 'Jane']
# => {:name => "Jane"}

Parameters:

  • (Hash)

Returns:

  • (Hash)


56
57
58
# File 'lib/transproc/hash.rb', line 56

def symbolize_keys(hash)
  symbolize_keys!(Hash[hash])
end

#symbolize_keys!(hash) ⇒ Object

Same as ‘:symbolize_keys` but mutates the hash

See Also:



65
66
67
# File 'lib/transproc/hash.rb', line 65

def symbolize_keys!(hash)
  map_keys!(hash, Transproc(:to_symbol).fn)
end

#unwrap(hash, root, keys = nil) ⇒ Hash

Collapse a nested hash from a specified key

Examples:

Transproc(:unwrap, :address, [:street, :zipcode])[address: { street: 'Street', zipcode: '123' }]
# => {street: "Street", zipcode: "123"}

Parameters:

  • (Hash)

Returns:

  • (Hash)


264
265
266
267
# File 'lib/transproc/hash.rb', line 264

def unwrap(hash, root, keys = nil)
  copy = Hash[hash].merge(root => Hash[hash[root]])
  unwrap!(copy, root, keys)
end

#unwrap!(hash, root, keys = nil) ⇒ Object

Same as ‘:unwrap` but mutates the hash

See Also:



274
275
276
277
278
279
280
281
282
# File 'lib/transproc/hash.rb', line 274

def unwrap!(hash, root, keys = nil)
  if nested_hash = hash[root]
    keys ||= nested_hash.keys
    hash.update(Hash[keys.zip(keys.map { |key| nested_hash.delete(key) })])
    hash.delete(root) if nested_hash.empty?
  end

  hash
end