class Dry::Transformer::Store

Immutable collection of named procedures from external modules

@api private

Attributes

methods[R]

@!attribute [r] methods

@return [Hash] The associated list of imported procedures

Public Class Methods

new(methods = {}) click to toggle source

@private

# File lib/dry/transformer/store.rb, line 25
def initialize(methods = {})
  @methods = methods.dup.freeze
  freeze
end

Public Instance Methods

contain?(key) click to toggle source

Returns wether the collection contains such procedure by its key

@param [Symbol] key

@return [Boolean]

# File lib/dry/transformer/store.rb, line 46
def contain?(key)
  methods.key?(key)
end
fetch(key) click to toggle source

Returns a procedure by its key in the collection

@param [Symbol] key

@return [Proc]

# File lib/dry/transformer/store.rb, line 36
def fetch(key)
  methods.fetch(key)
end
import(*args) click to toggle source

Imports proc(s) to the collection from another module

@private

# File lib/dry/transformer/store.rb, line 65
def import(*args)
  first = args.first
  return import_all(first) if first.instance_of?(Module)

  opts   = args.pop
  source = opts.fetch(:from)
  rename = opts.fetch(:as) { first.to_sym }

  return import_methods(source, args) if args.count > 1

  import_method(source, first, rename)
end
register(name, fn = nil, &block) click to toggle source

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

# File lib/dry/transformer/store.rb, line 57
def register(name, fn = nil, &block)
  self.class.new(methods.merge(name => fn || block))
end

Protected Instance Methods

import_all(source) click to toggle source

Creates new immutable collection from the current one, updated with all singleton methods and imported methods from the other module

@param [Module] source The module to import procedures from

@return [Dry::Transformer::Store]

# File lib/dry/transformer/store.rb, line 119
def import_all(source)
  names = source.public_methods - Registry.instance_methods - Module.methods
  names -= [:initialize] # for compatibility with Rubinius
  names += source.store.methods.keys if source.is_a? Registry

  import_methods(source, names)
end
import_method(source, name, new_name = name) click to toggle source

Creates new immutable collection from the current one, updated with either the module's singleton method, or the proc having been imported from another module.

@param [Module] source @param [Symbol] name @param [Symbol] new_name

@return [Dry::Transformer::Store]

# File lib/dry/transformer/store.rb, line 90
def import_method(source, name, new_name = name)
  from = name.to_sym
  to   = new_name.to_sym

  fn = source.is_a?(Registry) ? source.fetch(from) : source.method(from)
  self.class.new(methods.merge(to => fn))
end
import_methods(source, names) click to toggle source

Creates new immutable collection from the current one, updated with either the module's singleton methods, or the procs having been imported from another module.

@param [Module] source @param [Array<Symbol>] names

@return [Dry::Transformer::Store]

# File lib/dry/transformer/store.rb, line 107
def import_methods(source, names)
  names.inject(self) { |a, e| a.import_method(source, e) }
end