Module: Transproc

Defined in:
lib/transproc.rb,
lib/transproc/hash.rb,
lib/transproc/proc.rb,
lib/transproc/class.rb,
lib/transproc/store.rb,
lib/transproc/error.rb,
lib/transproc/array.rb,
lib/transproc/version.rb,
lib/transproc/registry.rb,
lib/transproc/composer.rb,
lib/transproc/function.rb,
lib/transproc/recursion.rb,
lib/transproc/coercions.rb,
lib/transproc/composite.rb,
lib/transproc/functions.rb,
lib/transproc/conditional.rb,
lib/transproc/support/deprecations.rb

Defined Under Namespace

Modules: ArrayTransformations, ClassTransformations, Coercions, Composer, Conditional, Deprecations, Functions, HashTransformations, Helper, ProcTransformations, Recursion, Registry Classes: Composite, Function, FunctionNotFoundError, Store

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.[](name, *args) ⇒ 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


42
43
44
45
46
47
48
49
50
# File 'lib/transproc.rb', line 42

def self.[](name, *args)
  fn = functions.fetch(name) { raise(FunctionNotFoundError, name) }

  if args.any?
    fn.with(*args)
  else
    fn
  end
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


14
15
16
# File 'lib/transproc.rb', line 14

def self.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))

29
30
31
32
33
34
35
# File 'lib/transproc.rb', line 29

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