Sha256: 8e2db79ae27daae4d3605dda851e589b66a1b8b7cc07d5c810432c99ae395675

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require 'dry/plugins/config'
require 'dry/plugins/dsl'

module Dry
  module Plugins
    class Registry
      # Build a plug-in registry for given `host`
      #
      # @see Builder#call
      class Builder
        def initialize(registry_method: Plugins.config.registry_method, **kwargs)
          @registry_method = registry_method
          super(**kwargs)
        end

        # @return [Symbol]
        attr_reader :registry_method

        # @param plugins [Module]
        #
        # @return [Registry]
        def call(host,
                 plugins: module_builder.call(host),
                 registry_class: class_builder.call(plugins))
          unless plugins.respond_to? registry_method
            registry_variable = :"@#{registry_method}"
            plugins.define_singleton_method registry_method do
              if instance_variable_defined? registry_variable
                instance_variable_get registry_variable
              else
                instance_variable_set registry_variable, registry_class.new(plugins)
              end
            end
          end

          unless plugins.singleton_class.included_modules.include? DSL
            plugins.extend DSL
          end

          plugins.public_send registry_method
        end

        private

        # @!method module_builder
        #   @return [ModuleBuilder]
        #
        # @!method class_builder
        #   @return [ClassBuilder]
        include Import[:module_builder, class_builder: :registry_class_builder]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-plugins-0.4.0 lib/dry/plugins/registry/builder.rb
dry-plugins-0.3.0 lib/dry/plugins/registry/builder.rb
dry-plugins-0.2.0 lib/dry/plugins/registry/builder.rb
dry-plugins-0.1.0 lib/dry/plugins/registry/builder.rb