Module: Transproc::ArrayTransformations
- Extended by:
- Functions
- Defined in:
- lib/transproc/array.rb
Overview
Transformation functions for Array objects
Instance Method Summary collapse
-
#group(array, key, keys) ⇒ Array
Group array values using provided root key and value keys.
-
#map_array(array, fn) ⇒ Array
Map array values using transformation function.
-
#map_array!(array, fn) ⇒ Object
Same as ‘map_array` but mutates the array.
-
#wrap(array, key, keys) ⇒ Array
Wrap array values using HashTransformations.nest function.
Methods included from Functions
Instance Method Details
#group(array, key, keys) ⇒ Array
Group array values using provided root key and value keys
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/transproc/array.rb', line 89 def group(array, key, keys) grouped = Hash.new { |h, k| h[k] = [] } array.each do |hash| hash = hash.dup child = {} keys.each { |k| child[k] = hash.delete(k) } grouped[hash] << child end grouped.map do |root, children| root.merge(key => children) end end |
#map_array(array, fn) ⇒ Array
Map array values using transformation function
39 40 41 |
# File 'lib/transproc/array.rb', line 39 def map_array(array, fn) map_array!(Array[*array], fn) end |
#map_array!(array, fn) ⇒ Object
Same as ‘map_array` but mutates the array
48 49 50 |
# File 'lib/transproc/array.rb', line 48 def map_array!(array, fn) array.map! { |value| fn[value] } end |
#wrap(array, key, keys) ⇒ Array
Wrap array values using HashTransformations.nest function
67 68 69 |
# File 'lib/transproc/array.rb', line 67 def wrap(array, key, keys) map_array(array, Transproc(:nest, key, keys)) end |