Sha256: 6b10524f6054d5ff87b3477bd46112301515f8275005cdf1a0e2a134ded15cf3

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require 'transproc/composite'

module Transproc
  # Transformation proc wrapper allowing composition of multiple procs into
  # a data-transformation pipeline.
  #
  # This is used by Transproc to wrap registered methods.
  #
  # @api private
  class Function
    # Wrapped proc or another composite function
    #
    # @return [Proc,Composed]
    #
    # @api private
    attr_reader :fn

    # Additional arguments that will be passed to the wrapped proc
    #
    # @return [Array]
    #
    # @api private
    attr_reader :args

    # @api private
    def initialize(fn, options)
      @fn = fn
      @args = options[:args]
    end

    # Call the wrapped proc
    #
    # @param [Object] value The input value
    #
    # @alias []
    #
    # @api public
    def call(*value)
      fn[*value, *args]
    rescue => e
      raise MalformedInputError.new(@fn, value, e)
    end
    alias_method :[], :call

    # Compose this function with another function or a proc
    #
    # @param [Proc,Function]
    #
    # @return [Composite]
    #
    # @alias :>>
    #
    # @api public
    def compose(other)
      Composite.new(self, other)
    end
    alias_method :+, :compose
    alias_method :>>, :compose

    # Return a simple AST representation of this function
    #
    # @return [Array]
    #
    # @api public
    def to_ast
      identifier = fn.instance_of?(Proc) ? fn : fn.name
      [identifier, args]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transproc-0.2.4 lib/transproc/function.rb