Module: Transproc

Defined in:
lib/transproc.rb,
lib/transproc/hash.rb,
lib/transproc/array.rb,
lib/transproc/class.rb,
lib/transproc/error.rb,
lib/transproc/object.rb,
lib/transproc/version.rb,
lib/transproc/composer.rb,
lib/transproc/function.rb,
lib/transproc/coercions.rb,
lib/transproc/composite.rb,
lib/transproc/recursion.rb,
lib/transproc/conditional.rb

Defined Under Namespace

Modules: ArrayTransformations, ClassTransformations, Coercions, Composer, Conditional, Functions, HashTransformations, Helper, ObjectTransformations, Recursion Classes: Composite, Function, MalformedInputError

Constant Summary collapse

Error =
Class.new(StandardError)
FunctionNotFoundError =
Class.new(Error)
FunctionAlreadyRegisteredError =
Class.new(Error)
VERSION =
'0.2.1'.freeze

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object

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.

Get registered function with provided name

Parameters:

  • name (Symbol)

    The name of the registered function



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

def [](name)
  functions.fetch(name) {
    raise FunctionNotFoundError, "no registered function for #{name}"
  }
end

.functionsObject

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.

Function registry



42
43
44
# File 'lib/transproc.rb', line 42

def functions
  @_functions ||= {}
end

.register(*args, &block) ⇒ Function

Register a new function

Examples:

Transproc.register(:to_json, -> v { v.to_json })

Transproc(:map_array, Transproc(:to_json))

Returns:



20
21
22
23
24
25
26
# File 'lib/transproc.rb', line 20

def register(*args, &block)
  name, fn = *args
  if functions.include?(name)
    raise FunctionAlreadyRegisteredError, "function #{name} is already defined"
  end
  functions[name] = fn || block
end