Class: Dry::Plugins::Registry

Inherits:
Object
  • Object
show all
Includes:
Container::Mixin
Defined in:
lib/dry/plugins/registry.rb,
lib/dry/plugins/registry/builder.rb,
lib/dry/plugins/registry/resolver.rb,
lib/dry/plugins/registry/key_error.rb,
lib/dry/plugins/registry/load_error.rb,
lib/dry/plugins/registry/class_builder.rb

Overview

Plug-in Registry

Defined Under Namespace

Classes: Builder, ClassBuilder, KeyError, LoadError, Resolver

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugins) ⇒ Registry

Returns a new instance of Registry.

Parameters:

  • plugins (Module)


22
23
24
25
26
# File 'lib/dry/plugins/registry.rb', line 22

def initialize(plugins)
  @require_path = Inflecto.underscore(plugins.to_s)
  @plugins = plugins
  super()
end

Instance Attribute Details

#pluginsModule (readonly)

Returns:

  • (Module)


29
30
31
# File 'lib/dry/plugins/registry.rb', line 29

def plugins
  @plugins
end

Instance Method Details

#proxy(plugin, name: nil) ⇒ Plugin

Parameters:

  • plugin (Module)
  • name (Symbol) (defaults to: nil)

Returns:



52
53
54
# File 'lib/dry/plugins/registry.rb', line 52

def proxy(plugin, name: nil)
  Plugin.new(self, name, plugin)
end

#register(key, plugin = nil) ⇒ Plugin

Parameters:

  • key (#to_sym)
  • plugin (Module, Plugin) (defaults to: nil)

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dry/plugins/registry.rb', line 35

def register(key, plugin = nil)
  if key.is_a?(Module)
    plugin = key
    key = Inflecto.underscore(Inflecto.demodulize(plugin.name))
  end
  key = key.to_s
  plugin = plugin.plugin if plugin.is_a? Plugin
  if key?(key) && resolve(key) != plugin
    raise Registry::KeyError.new(self, key, plugin)
  end
  super key, plugin
end

#resolve(name) ⇒ Plugin Also known as: []

Resolve an item from the container

Parameters:

  • name (Mixed)

    The key for the item you wish to resolve

Returns:



64
65
66
67
68
# File 'lib/dry/plugins/registry.rb', line 64

def resolve(name)
  registry = self
  plugin = config.resolver.call(_container, name, @require_path)
  Plugin.new(registry, name, plugin)
end