Module: Transproc::Registry

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 foo(name, prefix)
    [prefix, '_', name].join
  end
end

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

module BarMethods
  # extend Transproc::Registry
  include FooMethods

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

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

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(target) ⇒ 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.



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

def self.extended(target)
  target.extend(ClassMethods)
end

Instance Method Details

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

Builds the transproc function either from a Proc, or from the module method

Parameters:

  • fn (Proc, Symbol)

    Either a proc, or a name of the module’s function to be wrapped to transproc

  • args (Object, Array)

    Args to be carried by the transproc

Returns:



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

def [](fn, *args)
  fun = fn.is_a?(Proc) ? fn : method(fn).to_proc
  Transproc::Function.new(fun, args: args)
end

#uses(name, options = {}) ⇒ undefined

Forwards the named method (transproc) to another module

Allows using transprocs from other modules without including those modules as a whole

end

module Bar
   extend Transproc::Registry

   uses :foo, from: Foo, as: :baz
   uses :bar, from: Foo
end

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

Examples:

module Foo
  extend Transproc::Registry

  def foo(value)
    value.upcase
  end

  def bar(value)
    value.downcase
  end

Parameters:

  • name (String, Symbol)
  • [Class] (Hash)

    a customizable set of options

  • [String, (Hash)

    a customizable set of options

Returns:

  • (undefined)


88
89
90
91
92
# File 'lib/transproc/registry.rb', line 88

def uses(name, options = {})
  source   = options.fetch(:from)
  new_name = options.fetch(:as, name)
  define_method(new_name) { |*args| source.__send__(name, *args) }
end