Module: Transproc::Transformer::ClassInterface

Included in:
Transproc::Transformer
Defined in:
lib/transproc/transformer/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.



10
11
12
# File 'lib/transproc/transformer/class_interface.rb', line 10

def dsl
  @dsl
end

Instance Method Details

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

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

Examples:


class MyTransformer < Transproc::Transformer[Transproc]
end

Parameters:

Returns:



26
27
28
29
30
# File 'lib/transproc/transformer/class_interface.rb', line 26

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
Transproc::Transformer.container(Transproc)
# => Transproc

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

Parameters:

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

    The container to resolve transprocs from

Returns:



59
60
61
62
63
64
65
# File 'lib/transproc/transformer/class_interface.rb', line 59

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

#define!(&block) ⇒ Object



73
74
75
76
77
# File 'lib/transproc/transformer/class_interface.rb', line 73

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

#import(*args) ⇒ Object



68
69
70
# File 'lib/transproc/transformer/class_interface.rb', line 68

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.



33
34
35
36
37
38
39
# File 'lib/transproc/transformer/class_interface.rb', line 33

def inherited(subclass)
  super

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

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

#newObject



80
81
82
83
84
# File 'lib/transproc/transformer/class_interface.rb', line 80

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 < Transproc::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:



108
109
110
# File 'lib/transproc/transformer/class_interface.rb', line 108

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