Module: Transproc::Transformer::ClassInterface Private

Included in:
Transproc::Transformer
Defined in:
lib/transproc/transformer/class_interface.rb

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ 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.



110
111
112
113
114
115
116
117
# File 'lib/transproc/transformer/class_interface.rb', line 110

def method_missing(method, *args, &block)
  if container.contain?(method)
    args.push(define(&block)) if block_given?
    transformations << t(method, *args)
  else
    super
  end
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:



19
20
21
22
23
# File 'lib/transproc/transformer/class_interface.rb', line 19

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

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

    The container to resolve transprocs from

Returns:



48
49
50
51
52
53
54
55
# File 'lib/transproc/transformer/class_interface.rb', line 48

def container(container = ::Transproc::Undefined)
  if container == ::Transproc::Undefined
    ensure_container_presence!
    @container
  else
    @container = container
  end
end

#define { ... } ⇒ Function Also known as: build

Define an anonymous transproc derived from given Transformer Evaluates block with transformations and returns initialized transproc. Does not mutate original Transformer

Examples:


class MyTransformer < Transproc::Transformer[MyContainer]
end

transproc = MyTransformer.define do
  map_values t(:to_string)
end
transproc.call(a: 1, b: 2)
# => {a: '1', b: '2'}

Yields:

  • Block allowing to define transformations. The same as class level DSL

Returns:



77
78
79
80
81
# File 'lib/transproc/transformer/class_interface.rb', line 77

def define(&block)
  return transproc unless block_given?

  Class.new(self).tap { |klass| klass.instance_eval(&block) }.transproc
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.



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

def inherited(subclass)
  subclass.container(@container) if defined?(@container)
end

#respond_to_missing?(method, _include_private = false) ⇒ Boolean

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.

Returns:

  • (Boolean)


120
121
122
# File 'lib/transproc/transformer/class_interface.rb', line 120

def respond_to_missing?(method, _include_private = false)
  container.contain?(method) || super
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:



105
106
107
# File 'lib/transproc/transformer/class_interface.rb', line 105

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

#transprocObject

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.



125
126
127
# File 'lib/transproc/transformer/class_interface.rb', line 125

def transproc
  transformations.reduce(:>>)
end