Class: Dry::Transformer::Pipe

Inherits:
Object
  • Object
show all
Extended by:
ClassInterface
Defined in:
lib/dry/transformer/pipe.rb,
lib/dry/transformer/pipe/dsl.rb,
lib/dry/transformer/pipe/class_interface.rb

Overview

Pipe class for defining transprocs with a class DSL.

Examples:

require 'anima'
require 'dry/transformer/all'

class User
  include Anima.new(:name, :address)
end

class Address
  include Anima.new(:city, :street, :zipcode)
end

class UsersMapper < Dry::Transformer::Pipe
  map_array do
    symbolize_keys
    rename_keys user_name: :name
    nest :address, %i(city street zipcode)
    map_value :address do
      constructor_inject Address
    end
    constructor_inject User
  end
end

UsersMapper.new.call(
  [
    { 'user_name' => 'Jane',
      'city' => 'NYC',
      'street' => 'Street 1',
      'zipcode' => '123'
    }
  ]
)
# => [
  #<User
    name="Jane"
    address=#<Address city="NYC" street="Street 1" zipcode="123">>
]

Defined Under Namespace

Modules: ClassInterface Classes: DSL

Instance Attribute Summary collapse

Attributes included from ClassInterface

#dsl

Instance Method Summary collapse

Methods included from ClassInterface

[], container, define!, import, inherited, new, t

Instance Attribute Details

#transprocObject (readonly)



50
51
52
# File 'lib/dry/transformer/pipe.rb', line 50

def transproc
  @transproc
end

Instance Method Details

#call(input) ⇒ mixed

Execute the transformation pipeline with the given input.

Examples:


class SymbolizeKeys < Dry::Transformer
  symbolize_keys
end

SymbolizeKeys.new.call('name' => 'Jane')
# => {:name=>"Jane"}

Parameters:

  • input (mixed)

    The input to pass to the pipeline

Returns:

  • (mixed)

    output The output returned from the pipeline



68
69
70
# File 'lib/dry/transformer/pipe.rb', line 68

def call(input)
  transproc.call(input)
end