Sha256: 091bd2597172caf4c68e4a454d89e43fdfd1545d511594ce9f9cdc5c9f25a5c3

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require 'dry/plugins/config'
require 'dry/plugins/registry/key_error'
require 'dry/plugins/registry/resolver'
require 'dry/plugins/plugin'
require 'dry-container'
require 'dry-equalizer'

module Dry
  module Plugins
    # Plug-in Registry
    class Registry
      include Dry::Container::Mixin
      include Dry::Equalizer(:_container, :config, :plugins)

      configure do |config|
        config.resolver = Resolver.new
      end

      # @param plugins [Module]
      def initialize(plugins)
        @require_path = Inflecto.underscore(plugins.to_s)
        @plugins = plugins
        super()
      end

      # @return [Module]
      attr_reader :plugins

      # @param key [#to_sym]
      # @param plugin [Module, Plugin]
      #
      # @return [Plugin]
      def register(key, plugin)
        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

      # @param plugin [Module]
      # @param name [Symbol]
      #
      # @return [Plugin]
      def proxy(plugin, name: nil)
        Plugin.new(self, name, plugin)
      end

      # Resolve an item from the container
      #
      # @param name [Mixed]
      #   The key for the item you wish to resolve
      #
      # @return [Plugin]
      #
      # @api public
      def resolve(name)
        registry = self
        plugin = config.resolver.call(_container, name, @require_path)
        Plugin.new(registry, name, plugin)
      end

      # @!parse alias [] resolve
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-plugins-0.1.0 lib/dry/plugins/registry.rb