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

#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"}


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, -> s { s.upcase })['name' => 'jane']
# => {"name" => "JANE"}


159
160
161
# File 'lib/transproc/hash.rb', line 159

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:



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

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"}


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

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"}}


183
184
185
# File 'lib/transproc/hash.rb', line 183

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:



192
193
194
195
196
197
198
199
200
201
# File 'lib/transproc/hash.rb', line 192

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

#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"}


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"}


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"}


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) ⇒ Hash

Collapse a nested hash from a specified key

Examples:

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


214
215
216
217
# File 'lib/transproc/hash.rb', line 214

def unwrap(hash, root, keys)
  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:



224
225
226
227
228
229
230
231
232
# File 'lib/transproc/hash.rb', line 224

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