class Dry::System::Loader

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.component_dirs.loader = MyLoader
  end
end

@api public

Public Class Methods

call(component, *args) click to toggle source

Returns an instance of the component

Provided optional args are passed to object's constructor

@param [Array] args Optional constructor args

@return [Object]

@api public

# File lib/dry/system/loader.rb, line 44
def call(component, *args)
  require!(component)

  constant = self.constant(component)

  if singleton?(constant)
    constant.instance(*args)
  else
    constant.new(*args)
  end
end
constant(component) click to toggle source

Returns the component's class constant

@return [Class]

@api public

# File lib/dry/system/loader.rb, line 62
def constant(component)
  inflector = component.inflector
  inflector.constantize(inflector.camelize(component.const_path))
end
require!(component) click to toggle source

Requires the component's source file

@api public

# File lib/dry/system/loader.rb, line 30
def require!(component)
  require(component.require_path)
  self
end

Private Class Methods

singleton?(constant) click to toggle source
# File lib/dry/system/loader.rb, line 69
def singleton?(constant)
  constant.respond_to?(:instance) && !constant.respond_to?(:new)
end