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, inflector = Dry::Inflector.new) ⇒ 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.



35
36
37
38
# File 'lib/dry/system/loader.rb', line 35

def initialize(path, inflector = Dry::Inflector.new)
  @path = path
  @inflector = inflector
end

Instance Attribute Details

#inflectorObject (readonly)



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

def inflector
  @inflector
end

#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)


49
50
51
52
53
54
55
# File 'lib/dry/system/loader.rb', line 49

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

#constantClass

Return component’s class constant

Returns:

  • (Class)


62
63
64
# File 'lib/dry/system/loader.rb', line 62

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