Sha256: 740bb4ab7a9e3c80a27651909a7a8dcb2602279600267d42a61baea497ce0f5e

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require "dry/system/constants"

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:, &block)
        register(
          name: name,
          group: group,
          source: Provider::Source.for(
            name: name,
            group: group,
            &block
          )
        )
      end

      def resolve(name:, group:)
        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

3 entries across 3 versions & 1 rubygems

Version Path
dry-system-1.0.1 lib/dry/system/provider_source_registry.rb
dry-system-1.0.0 lib/dry/system/provider_source_registry.rb
dry-system-1.0.0.rc1 lib/dry/system/provider_source_registry.rb