Sha256: de6ee8eb7dc1d771adf3a829730d36af4cc8b67d6f6c54b6587368705482f01e

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

require 'inflecto'

module Dry
  module System
    # Default component loader implementation
    #
    # This class is configured by default for every System::Container. You can
    # provide your own and use it in your containers too.
    #
    # @example
    #   class MyLoader < Dry::System::Loader
    #     def call(*args)
    #       constant.build(*args)
    #     end
    #   end
    #
    #   class MyApp < Dry::System::Container
    #     configure do |config|
    #       # ...
    #       config.loader MyLoader
    #     end
    #   end
    #
    # @api public
    class Loader
      # @!attribute [r] path
      #   @return [String] Path to component's file
      attr_reader :path

      # @api private
      def initialize(path)
        @path = path
      end

      # Returns component's instance
      #
      # Provided optional args are passed to object's constructor
      #
      # @param [Array] *args Optional constructor args
      #
      # @return [Object]
      #
      # @api public
      def call(*args)
        if singleton?(constant)
          constant.instance(*args)
        else
          constant.new(*args)
        end
      end

      # Return component's class constant
      #
      # @return [Class]
      #
      # @api public
      def constant
        Inflecto.constantize(Inflecto.camelize(path))
      end

      private

      # @api private
      def singleton?(constant)
        constant.respond_to?(:instance) && !constant.respond_to?(:new)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dry-system-0.8.1 lib/dry/system/loader.rb
dry-system-0.8.0 lib/dry/system/loader.rb
dry-system-0.7.3 lib/dry/system/loader.rb
dry-system-0.7.2 lib/dry/system/loader.rb
dry-system-0.7.1 lib/dry/system/loader.rb
dry-system-0.7.0 lib/dry/system/loader.rb
dry-system-0.6.0 lib/dry/system/loader.rb
dry-system-0.5.1 lib/dry/system/loader.rb
dry-system-0.5.0 lib/dry/system/loader.rb