Module: Dry::Transformer::Pipe::ClassInterface

Included in:
Dry::Transformer::Pipe
Defined in:
lib/dry/transformer/pipe/class_interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dslObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/dry/transformer/pipe/class_interface.rb', line 11

def dsl
  @dsl
end

Instance Method Details

#[](container) ⇒ subclass of Dry::Transformer

Return a base Dry::Transformer class with the container configured to the passed argument.

Examples:


class MyTransformer < Dry::Transformer[Transproc]
end

Parameters:

  • container (Transproc::Registry)

    The container to resolve transprocs from

Returns:



27
28
29
30
31
# File 'lib/dry/transformer/pipe/class_interface.rb', line 27

def [](container)
  klass = Class.new(self)
  klass.container(container)
  klass
end

#container(container = Undefined) ⇒ Transproc::Registry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get or set the container to resolve transprocs from.

Examples:


# Setter
Dry::Transformer.container(Transproc)
# => Transproc

# Getter
Dry::Transformer.container
# => Transproc

Parameters:

  • container (Transproc::Registry) (defaults to: Undefined)

    The container to resolve transprocs from

Returns:

  • (Transproc::Registry)


60
61
62
63
64
65
66
# File 'lib/dry/transformer/pipe/class_interface.rb', line 60

def container(container = Undefined)
  if container.equal?(Undefined)
    @container ||= Module.new.extend(Dry::Transformer::Registry)
  else
    @container = container
  end
end

#define!(&block) ⇒ Object



74
75
76
77
78
# File 'lib/dry/transformer/pipe/class_interface.rb', line 74

def define!(&block)
  @dsl ||= DSL.new(container)
  @dsl.instance_eval(&block)
  self
end

#import(*args) ⇒ Object



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

def import(*args)
  container.import(*args)
end

#inherited(subclass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
37
38
39
40
# File 'lib/dry/transformer/pipe/class_interface.rb', line 34

def inherited(subclass)
  super

  subclass.container(@container) if defined?(@container)

  subclass.instance_variable_set("@dsl", dsl.dup) if dsl
end

#newObject



81
82
83
84
85
# File 'lib/dry/transformer/pipe/class_interface.rb', line 81

def new(*)
  super.tap do |transformer|
    transformer.instance_variable_set("@transproc", dsl.(transformer)) if dsl
  end
end

#t(fn, *args) ⇒ Transproc::Function

Get a transformation from the container, without adding it to the transformation pipeline

Examples:


class Stringify < Dry::Transformer
  map_values t(:to_string)
end

Stringify.new.call(a: 1, b: 2)
# => {a: '1', b: '2'}

Parameters:

  • fn (Proc, Symbol)

    A proc, a name of the module’s own function, or a name of imported procedure from another module

  • args (Object, Array)

    Args to be carried by the transproc

Returns:

  • (Transproc::Function)


109
110
111
# File 'lib/dry/transformer/pipe/class_interface.rb', line 109

def t(fn, *args)
  container[fn, *args]
end