Module: Transproc::Registry

Included in:
ArrayTransformations, ClassTransformations, Coercions, Conditional, HashTransformations, Recursion
Defined in:
lib/transproc/registry.rb

Overview

Container to define transproc functions in, and access them via ‘[]` method from the outside of the module

Examples:

module FooMethods
  extend Transproc::Registry

  def self.foo(name, prefix)
    [prefix, '_', name].join
  end
end

fn = FooMethods[:foo, 'baz']
fn['qux'] # => 'qux_baz'

module BarMethods
  extend FooMethods

  def self.bar(*args)
    foo(*args).upcase
  end
end

fn = BarMethods[:foo, 'baz']
fn['qux'] # => 'qux_baz'

fn = BarMethods[:bar, 'baz']
fn['qux'] # => 'QUX_BAZ'

Instance Method Summary collapse

Instance Method Details

#[](fn, *args) ⇒ Transproc::Function Also known as: t

Builds the transformation

Parameters:

  • fn (Proc, Symbol)

    A proc, a name of the module’s own function, or a name of imported procedure from another module

  • args (Object, Array)

    Args to be carried by the transproc

Returns:



47
48
49
# File 'lib/transproc/registry.rb', line 47

def [](fn, *args)
  Function.new(fetch(fn), args: args, name: fn)
end

#fetch(fn) ⇒ #call

Gets the procedure for creating a transproc

Parameters:

  • fn (#call, Symbol)

    Either the procedure, or the name of the method of the current module, or the registered key of imported procedure in a store.

Returns:

  • (#call)


117
118
119
120
121
122
# File 'lib/transproc/registry.rb', line 117

def fetch(fn)
  return fn unless fn.instance_of? Symbol
  respond_to?(fn) ? method(fn) : store.fetch(fn)
rescue
  raise FunctionNotFoundError.new(fn, self)
end

#import(source, options = nil) ⇒ itself Also known as: uses

Imports either a method (converted to a proc) from another module, or all methods from that module.

If the external module is a registry, looks for its imports too.

end

module Qux
  def self.qux(value)
    value.reverse
  end
end

module Bar
   extend Transproc::Registry

   import :foo, from: Foo, as: :baz
   import :bar, from: Foo
   import Qux
end

Bar[:baz]['Qux'] # => 'QUX'
Bar[:bar]['Qux'] # => 'qux'
Bar[:qux]['Qux'] # => 'xuQ'

Examples:

module Foo
  def self.foo(value)
    value.upcase
  end

  def self.bar(value)
    value.downcase
  end

Parameters:

  • name (Module, #to_sym)
  • [Module] (Hash)

    a customizable set of options

  • [#to_sym] (Hash)

    a customizable set of options

Returns:

  • (itself)

    self



95
96
97
98
# File 'lib/transproc/registry.rb', line 95

def import(source, options = nil)
  @store = store.import(source, options)
  self
end

#storeTransproc::Store

The store of procedures imported from external modules

Returns:



105
106
107
# File 'lib/transproc/registry.rb', line 105

def store
  @store ||= Store.new
end