Sha256: 6001f83d295afbb69a83fa7f147fba248ac157159ebff7fa598bfc5866b1f8c4

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

require 'transproc/version'
require 'transproc/function'
require 'transproc/functions'
require 'transproc/composer'
require 'transproc/error'
require 'transproc/store'
require 'transproc/registry'
require 'transproc/transformer'
require 'transproc/support/deprecations'

module Transproc
  Undefined = Object.new.freeze
  Transformer.container(self)
  # Function registry
  #
  # @api private
  def self.functions
    @_functions ||= {}
  end

  # Register a new function
  #
  # @example
  #   Transproc.register(:to_json, -> v { v.to_json })
  #
  #   Transproc(:map_array, Transproc(:to_json))
  #
  #
  # @return [Function]
  #
  # @api public
  def self.register(*args, &block)
    name, fn = *args
    if functions.include? name
      raise FunctionAlreadyRegisteredError, "Function #{name} is already defined"
    end
    functions[name] = fn || block
  end

  # Get registered function with provided name
  #
  # @param [Symbol] name The name of the registered function
  #
  # @api private
  def self.[](name, *args)
    fn = functions.fetch(name) { raise(FunctionNotFoundError, name) }

    if args.any?
      fn.with(*args)
    else
      fn
    end
  end
end

require 'transproc/array'
require 'transproc/hash'

# 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)
  Transproc::Deprecations.announce(
    'Transproc()',
    'Define your own function registry using Transproc::Registry extension'
  )

  case fn
  when Proc then Transproc::Function.new(fn, args: args)
  when Symbol
    func = Transproc[fn, *args]
    case func
    when Transproc::Function, Transproc::Composite then func
    else Transproc::Function.new(func, args: args)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transproc-0.4.2 lib/transproc.rb
transproc-0.4.1 lib/transproc.rb