Sha256: cc6777f23126ed2f2d1204570b4cd78e5b595dca6f9fdb8a5f424cf6e73e83dd

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require "dry/core/deprecations"
require_relative "constants"
require_relative "provider/source"

module Dry
  module System
    # @api private
    class ProviderSourceRegistry
      attr_reader :sources

      def initialize
        @sources = {}
      end

      def load_sources(path)
        Dir[File.join(path, "**/#{RB_GLOB}")].sort.each do |file|
          require file
        end
      end

      def register(name:, group:, source:)
        sources[key(name, group)] = source
      end

      def register_from_block(name:, group:, target_container:, &block)
        register(
          name: name,
          group: group,
          source: Provider::Source.for(
            name: name,
            group: group,
            target_container: target_container,
            &block
          )
        )
      end

      def resolve(name:, group:)
        if group == :system
          Dry::Core::Deprecations.announce(
            "Providers using `from: :system`",
            "Use `from: :dry_system` instead",
            tag: "dry-system",
            uplevel: 1
          )

          group = :dry_system
        end

        sources[key(name, group)].tap { |source|
          unless source
            raise ProviderSourceNotFoundError.new(
              name: name,
              group: group,
              keys: sources.keys
            )
          end
        }
      end

      private

      def key(name, group)
        {group: group, name: name}
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-system-0.26.0 lib/dry/system/provider_source_registry.rb
dry-system-0.25.0 lib/dry/system/provider_source_registry.rb
dry-system-0.24.0 lib/dry/system/provider_source_registry.rb
dry-system-0.23.0 lib/dry/system/provider_source_registry.rb