module Dry::Transformer::Registry

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

@example

module FooMethods
  extend Dry::Transformer::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'

@api public

Public Instance Methods

[](fn, *args) click to toggle source

Builds the transformation

@param [Proc, Symbol] fn

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

@param [Object, Array] args

Args to be carried by the transproc

@return [Dry::Transformer::Function]

@alias :t

# File lib/dry/transformer/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
Also aliased as: t
contain?(key) click to toggle source

Returns wether the registry contains such transformation by its key

@param [Symbol] key

@return [Boolean]

# File lib/dry/transformer/registry.rb, line 63
def contain?(key)
  respond_to?(key) || store.contain?(key)
end
fetch(fn) click to toggle source

Gets the procedure for creating a transproc

@param [#call, Symbol] fn

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

@return [#call]

# File lib/dry/transformer/registry.rb, line 134
def fetch(fn)
  return fn unless fn.instance_of? Symbol

  respond_to?(fn) ? method(fn) : store.fetch(fn)
rescue StandardError
  raise FunctionNotFoundError.new(fn, self)
end
import(*args) click to toggle source

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.

@overload import(source)

Loads all methods from the source object

@param [Object] source

@overload import(*names, **options)

Loads selected methods from the source object

@param [Array<Symbol>] names
@param [Hash] options
@options options [Object] :from The source object

@overload import(name, **options)

Loads selected methods from the source object

@param [Symbol] name
@param [Hash] options
@options options [Object] :from The source object
@options options [Object] :as The new name for the transformation

@return [itself] self

@alias :import

# File lib/dry/transformer/registry.rb, line 112
def import(*args)
  @store = store.import(*args)
  self
end
Also aliased as: uses
register(name, fn = nil, &block) click to toggle source

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

# File lib/dry/transformer/registry.rb, line 74
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
store() click to toggle source

The store of procedures imported from external modules

@return [Dry::Transformer::Store]

# File lib/dry/transformer/registry.rb, line 122
def store
  @store ||= Store.new
end
t(fn, *args)
Alias for: []
uses(*args)
Alias for: import

Private Instance Methods

already_wrapped?(func) click to toggle source

@api private

# File lib/dry/transformer/registry.rb, line 145
def already_wrapped?(func)
  func.is_a?(Dry::Transformer::Function) || func.is_a?(Dry::Transformer::Composite)
end