Class: Transproc::Function Private

Inherits:
Object
  • Object
show all
Defined in:
lib/transproc/function.rb

Overview

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

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

This is used by Transproc to wrap registered methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn, options = {}) ⇒ Function

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 a new instance of Function


33
34
35
36
37
# File 'lib/transproc/function.rb', line 33

def initialize(fn, options = {})
  @fn = fn
  @args = options.fetch(:args, [])
  @name = options.fetch(:name, fn)
end

Instance Attribute Details

#argsArray (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.

Additional arguments that will be passed to the wrapped proc

Returns:

  • (Array)

23
24
25
# File 'lib/transproc/function.rb', line 23

def args
  @args
end

#fnProc, Composed (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.

Wrapped proc or another composite function

Returns:

  • (Proc, Composed)

16
17
18
# File 'lib/transproc/function.rb', line 16

def fn
  @fn
end

#name<type] The name of the function (readonly)

Returns <type] The name of the function

Returns:

  • (<type] The name of the function)

    <type] The name of the function


30
31
32
# File 'lib/transproc/function.rb', line 30

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?


76
77
78
79
# File 'lib/transproc/function.rb', line 76

def ==(other)
  return false unless other.instance_of?(self.class)
  [fn, name, args] == [other.fn, other.name, other.args]
end

#call(*value) ⇒ Object Also known as: []

Call the wrapped proc

Parameters:

  • value (Object)

    The input value


46
47
48
# File 'lib/transproc/function.rb', line 46

def call(*value)
  fn.call(*value, *args)
end

#compose(other) ⇒ Composite Also known as: +, >>

Compose this function with another function or a proc

Parameters:

Returns:


60
61
62
# File 'lib/transproc/function.rb', line 60

def compose(other)
  Composite.new(self, other)
end

#to_astArray

Return a simple AST representation of this function

Returns:

  • (Array)

87
88
89
# File 'lib/transproc/function.rb', line 87

def to_ast
  [name, args]
end

#to_procProc

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.

Converts a transproc to a simple proc

Returns:

  • (Proc)

95
96
97
98
99
100
101
# File 'lib/transproc/function.rb', line 95

def to_proc
  if args.size > 0
    proc { |*value| fn.call(*value, *args) }
  else
    fn.to_proc
  end
end

#with(*args) ⇒ Function

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.

Return a new fn with curried args

Returns:


71
72
73
# File 'lib/transproc/function.rb', line 71

def with(*args)
  self.class.new(fn, name: name, args: args)
end