Class: Dry::System::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/system/loader.rb

Overview

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.

Examples:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Loader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Loader.



31
32
33
# File 'lib/dry/system/loader.rb', line 31

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)



28
29
30
# File 'lib/dry/system/loader.rb', line 28

def path
  @path
end

Instance Method Details

#call(*args) ⇒ Object

Returns component's instance

Provided optional args are passed to object's constructor

Parameters:

  • *args (Array)

    Optional constructor args

Returns:

  • (Object)


44
45
46
47
48
49
50
# File 'lib/dry/system/loader.rb', line 44

def call(*args)
  if singleton?(constant)
    constant.instance(*args)
  else
    constant.new(*args)
  end
end

#constantClass

Return component's class constant

Returns:

  • (Class)


57
58
59
# File 'lib/dry/system/loader.rb', line 57

def constant
  Inflecto.constantize(Inflecto.camelize(path))
end