Sha256: 66ddf10e7cf779f303299123bff8d4ee83eaf08b878f4f495bbab792852b0011

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require 'concurrent/map'
require 'dry/system/constants'
require 'dry/system/components/bootable'

module Dry
  module System
    class Provider
      attr_reader :identifier

      attr_reader :options

      attr_reader :components

      def initialize(identifier, options)
        @identifier = identifier
        @options = options
        @components = Concurrent::Map.new
      end

      def boot_path
        options.fetch(:boot_path)
      end

      def boot_files
        ::Dir[boot_path.join("**/#{RB_GLOB}")].sort
      end

      def register_component(name, fn)
        components[name] = Components::Bootable.new(name, &fn)
      end

      def boot_file(name)
        boot_files.detect { |path| Pathname(path).basename(RB_EXT).to_s == name.to_s }
      end

      def component(name, options = {})
        identifier = options[:key] || name
        components.fetch(identifier).new(name, options)
      end

      def load_components
        boot_files.each { |f| require f }
        freeze
        self
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-system-0.14.0 lib/dry/system/provider.rb
dry-system-0.13.2 lib/dry/system/provider.rb
dry-system-0.13.1 lib/dry/system/provider.rb
dry-system-0.13.0 lib/dry/system/provider.rb