class Dry::Transformer::Function

Transformation proc wrapper allowing composition of multiple procs into a data-transformation pipeline.

This is used by Dry::Transformer to wrap registered methods.

@api private

Attributes

args[R]

Additional arguments that will be passed to the wrapped proc

@return [Array]

@api private

fn[R]

Wrapped proc or another composite function

@return [Proc,Composed]

@api private

name[R]

@!attribute [r] name

@return [<type] The name of the function

@api public

Public Class Methods

new(fn, options = {}) click to toggle source

@api private

# File lib/dry/transformer/function.rb, line 36
def initialize(fn, options = {})
  @fn = fn
  @args = options.fetch(:args, [])
  @name = options.fetch(:name, fn)
end

Public Instance Methods

+(other)
Alias for: compose
==(other) click to toggle source

@api public

# File lib/dry/transformer/function.rb, line 79
def ==(other)
  return false unless other.instance_of?(self.class)

  [fn, name, args] == [other.fn, other.name, other.args]
end
Also aliased as: eql?
>>(other)
Alias for: compose
[](*value)
Alias for: call
call(*value) click to toggle source

Call the wrapped proc

@param [Object] value The input value

@alias []

@api public

# File lib/dry/transformer/function.rb, line 49
def call(*value)
  fn.call(*value, *args)
end
Also aliased as: []
compose(other) click to toggle source

Compose this function with another function or a proc

@param [Proc,Function]

@return [Composite]

@alias :>>

@api public

# File lib/dry/transformer/function.rb, line 63
def compose(other)
  Composite.new(self, other)
end
Also aliased as: +, >>
eql?(other)
Alias for: ==
to_ast() click to toggle source

Return a simple AST representation of this function

@return [Array]

@api public

# File lib/dry/transformer/function.rb, line 91
def to_ast
  args_ast = args.map { |arg| arg.respond_to?(:to_ast) ? arg.to_ast : arg }
  [name, args_ast]
end
to_proc() click to toggle source

Converts a transproc to a simple proc

@return [Proc]

# File lib/dry/transformer/function.rb, line 100
def to_proc
  if !args.empty?
    proc { |*value| fn.call(*value, *args) }
  else
    fn.to_proc
  end
end
with(*args) click to toggle source

Return a new fn with curried args

@return [Function]

@api private

# File lib/dry/transformer/function.rb, line 74
def with(*args)
  self.class.new(fn, name: name, args: args)
end