Module: Dry::Transformer::ArrayTransformations

Extended by:
Registry
Defined in:
lib/dry/transformer/array_transformations.rb,
lib/dry/transformer/array_transformations/combine.rb

Overview

Transformation functions for Array objects

Examples:

require 'dry/transformer/array'

include Dry::Transformer::Helper

fn = t(:map_array, t(:symbolize_keys)) >> t(:wrap, :address, [:city, :zipcode])

fn.call(
  [
    { 'city' => 'Boston', 'zipcode' => '123' },
    { 'city' => 'NYC', 'zipcode' => '312' }
  ]
)
# => [{:address=>{:city=>"Boston", :zipcode=>"123"}}, {:address=>{:city=>"NYC", :zipcode=>"312"}}]

Defined Under Namespace

Classes: Combine

Class Method Summary collapse

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

Parameters:

  • keys (Array)

Returns:

  • (Array)


173
174
175
176
# File 'lib/dry/transformer/array_transformations.rb', line 173

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



121
122
123
# File 'lib/dry/transformer/array_transformations.rb', line 121

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

Examples:

fn = t(:extract_key, :name)
fn.call [
  { name: 'Alice', role: 'sender' },
  { name: 'Bob', role: 'receiver' },
  { role: 'listener' }
]
# => ['Alice', 'Bob', nil]

Parameters:

  • array (Array<Hash>)

    The input array of hashes

  • key (Object)

    The key to extract values by

Returns:

  • (Array)


142
143
144
# File 'lib/dry/transformer/array_transformations.rb', line 142

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

Examples:

fn = Dry::Transformer(:group, :tags, [:tag])

fn.call [
  { task: 'Group it', tag: 'task' },
  { task: 'Group it', tag: 'important' }
]
# => [{ task: 'Group it', tags: [{ tag: 'task' }, { tag: 'important' }]]

Parameters:

  • array (Array)

    The input array

  • key (Object)

    The nesting root key

  • keys (Object)

    The nesting value keys

Returns:

  • (Array)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dry/transformer/array_transformations.rb', line 82

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`.

Examples:

fn = t(:insert_key, 'name')
fn.call ['Alice', 'Bob', nil]
# => [{ 'name' => 'Alice' }, { 'name' => 'Bob' }, { 'name' => nil }]

Parameters:

  • array (Array<Hash>)

    The input array of hashes

  • key (Object)

    The key to extract values by

Returns:

  • (Array)


161
162
163
# File 'lib/dry/transformer/array_transformations.rb', line 161

def self.insert_key(array, key)
  map_array(array, ->(v) { {key => v} })
end

.map_array(array, fn) ⇒ Array

Map array values using transformation function

Examples:


fn = Dry::Transformer(:map_array, -> v { v.upcase })

fn.call ['foo', 'bar'] # => ["FOO", "BAR"]

Parameters:

  • array (Array)

    The input array

  • fn (Proc)

    The transformation function

Returns:

  • (Array)


40
41
42
# File 'lib/dry/transformer/array_transformations.rb', line 40

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

Examples:

fn = Dry::Transformer(:ungroup, :tags, [:tag])

fn.call [
  { task: 'Group it', tags: [{ tag: 'task' }, { tag: 'important' }] }
]
# => [
  { task: 'Group it', tag: 'task' },
  { task: 'Group it', tag: 'important' }
]

Parameters:

  • array (Array)

    The input array

  • key (Object)

    The nesting root key

  • keys (Object)

    The nesting value keys

Returns:

  • (Array)


117
118
119
# File 'lib/dry/transformer/array_transformations.rb', line 117

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

Examples:

fn = Dry::Transformer(:wrap, :address, [:city, :zipcode])

fn.call [{ city: 'NYC', zipcode: '123' }]
# => [{ address: { city: 'NYC', zipcode: '123' } }]

Parameters:

  • array (Array)

    The input array

  • key (Object)

    The nesting root key

  • keys (Object)

    The nesting value keys

Returns:

  • (Array)


59
60
61
62
# File 'lib/dry/transformer/array_transformations.rb', line 59

def self.wrap(array, key, keys)
  nest = HashTransformations[:nest, key, keys]
  array.map { |element| nest.call(element) }
end