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.



37
38
39
40
# File 'lib/dry/system/loader.rb', line 37

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

Instance Attribute Details

#inflectorObject (readonly)



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

def inflector
  @inflector
end

#pathObject (readonly)



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

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)


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

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

#constantClass

Return component’s class constant

Returns:

  • (Class)


64
65
66
# File 'lib/dry/system/loader.rb', line 64

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