Sha256: 780095ef30947095c166d9c3a3e862d4b5b25f39d30c960c7a44ca0f89bc4246

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'transproc/version'
require 'transproc/function'
require 'transproc/composer'

module Transproc
  module_function

  # Register a new function
  #
  # @example
  #   Transproc.register(:to_json, -> v { v.to_json })
  #
  #   Transproc(:map_array, Transproc(:to_json))
  #
  #
  # @return [Function]
  #
  # @api public
  def register(*args, &block)
    name, fn = *args
    functions[name] = fn || block
  end

  # Get registered function with provided name
  #
  # @param [Symbol] name The name of the registered function
  #
  # @api private
  def [](name)
    functions.fetch(name)
  end

  # Function registry
  #
  # @api private
  def functions
    @_functions ||= {}
  end

  # Function container extension
  #
  # @example
  #   module MyTransformations
  #     extend Transproc::Functions
  #
  #     def boom!(value)
  #       "#{value} BOOM!"
  #     end
  #   end
  #
  #   Transproc(:boom!)['w00t!'] # => "w00t! BOOM!"
  #
  # @api public
  module Functions
    def method_added(meth)
      module_function meth
      Transproc.register(meth, method(meth))
    end
  end
end

# Access registered functions
#
# @example
#   Transproc(:map_array, Transproc(:to_string))
#
#   Transproc(:to_string) >> Transproc(-> v { v.upcase })
#
# @param [Symbol,Proc] fn The name of the registered function or an anonymous proc
# @param [Array] args Optional addition args that a given function may need
#
# @return [Function]
#
# @api public
def Transproc(fn, *args)
  case fn
  when Proc then Transproc::Function.new(fn, args: args)
  when Symbol then Transproc::Function.new(Transproc[fn], args: args)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transproc-0.2.0 lib/transproc.rb