Module: Dry::Transformer::ArrayTransformations
- Extended by:
- Registry
- Defined in:
- lib/dry/transformer/array.rb,
lib/dry/transformer/array/combine.rb
Overview
Transformation functions for Array objects
Defined Under Namespace
Classes: Combine
Class Method Summary collapse
-
.add_keys(array, keys) ⇒ Array
Adds missing keys with nil value to all tuples in array.
- .combine(array, mappings) ⇒ Object
-
.extract_key(array, key) ⇒ Array
Converts the array of hashes to array of values, extracted by given key.
-
.group(array, key, keys) ⇒ Array
Group array values using provided root key and value keys.
-
.insert_key(array, key) ⇒ Array
Wraps every value of the array to tuple with given key.
-
.map_array(array, fn) ⇒ Array
Map array values using transformation function.
-
.ungroup(array, key, keys) ⇒ Array
Ungroup array values using provided root key and value keys.
-
.wrap(array, key, keys) ⇒ Array
Wrap array values using HashTransformations.nest function.
Methods included from Registry
[], contain?, fetch, import, register, store
Class Method Details
.add_keys(array, keys) ⇒ Array
Adds missing keys with nil value to all tuples in array
177 178 179 180 |
# File 'lib/dry/transformer/array.rb', line 177 def self.add_keys(array, keys) base = keys.inject({}) { |a, e| a.merge(e => nil) } map_array(array, ->(v) { base.merge(v) }) end |
.combine(array, mappings) ⇒ Object
125 126 127 |
# File 'lib/dry/transformer/array.rb', line 125 def self.combine(array, mappings) Combine.combine(array, mappings) end |
.extract_key(array, key) ⇒ Array
Converts the array of hashes to array of values, extracted by given key
146 147 148 |
# File 'lib/dry/transformer/array.rb', line 146 def self.extract_key(array, key) map_array(array, ->(v) { v[key] }) end |
.group(array, key, keys) ⇒ Array
Group array values using provided root key and value keys
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/dry/transformer/array.rb', line 86 def self.group(array, key, keys) grouped = Hash.new { |h, k| h[k] = [] } array.each do |hash| hash = Hash[hash] old_group = Coercions.to_tuples(hash.delete(key)) new_group = keys.inject({}) { |a, e| a.merge(e => hash.delete(e)) } grouped[hash] << old_group.map { |item| item.merge(new_group) } end grouped.map do |root, children| root.merge(key => children.flatten) end end |
.insert_key(array, key) ⇒ Array
Wraps every value of the array to tuple with given key
The transformation partially inverses the ‘extract_key`.
165 166 167 |
# File 'lib/dry/transformer/array.rb', line 165 def self.insert_key(array, key) map_array(array, ->(v) { { key => v } }) end |
.map_array(array, fn) ⇒ Array
Map array values using transformation function
44 45 46 |
# File 'lib/dry/transformer/array.rb', line 44 def self.map_array(array, fn) Array(array).map { |value| fn[value] } end |
.ungroup(array, key, keys) ⇒ Array
Ungroup array values using provided root key and value keys
121 122 123 |
# File 'lib/dry/transformer/array.rb', line 121 def self.ungroup(array, key, keys) array.flat_map { |item| HashTransformations.split(item, key, keys) } end |
.wrap(array, key, keys) ⇒ Array
Wrap array values using HashTransformations.nest function
63 64 65 66 |
# File 'lib/dry/transformer/array.rb', line 63 def self.wrap(array, key, keys) nest = HashTransformations[:nest, key, keys] array.map { |element| nest.call(element) } end |