Module: Transproc::Registry

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:



48
49
50
51
52
53
# File 'lib/transproc/registry.rb', line 48

def [](fn, *args)
  fetched = fetch(fn)

  return Function.new(fetched, args: args, name: fn) unless already_wrapped?(fetched)
  args.empty? ? fetched : fetched.with(*args)
end

#contain?(key) ⇒ Boolean

Returns wether the registry contains such transformation by its key

Parameters:

  • key (Symbol)

Returns:

  • (Boolean)


62
63
64
# File 'lib/transproc/registry.rb', line 62

def contain?(key)
  respond_to?(key) || store.contain?(key)
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)


132
133
134
135
136
137
# File 'lib/transproc/registry.rb', line 132

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) ⇒ itself #import(*names, **options) ⇒ itself #import(name, **options) ⇒ 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.

Overloads:

  • #import(source) ⇒ itself

    Loads all methods from the source object

    Parameters:

    • source (Object)
  • #import(*names, **options) ⇒ itself

    Loads selected methods from the source object

    Parameters:

    • names (Array<Symbol>)
    • options (Hash)
  • #import(name, **options) ⇒ itself

    Loads selected methods from the source object

    Parameters:

    • name (Symbol)
    • options (Hash)

Returns:

  • (itself)

    self



110
111
112
113
# File 'lib/transproc/registry.rb', line 110

def import(*args)
  @store = store.import(*args)
  self
end

#register(name, fn = nil, &block) ⇒ Object

store.register(:to_json) { |v| v.to_json }



73
74
75
76
77
78
79
# File 'lib/transproc/registry.rb', line 73

def register(name, fn = nil, &block)
  if contain?(name)
    raise FunctionAlreadyRegisteredError, "Function #{name} is already defined"
  end
  @store = store.register(name, fn, &block)
  self
end

#storeTransproc::Store

The store of procedures imported from external modules

Returns:



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

def store
  @store ||= Store.new
end