Class: Dry::System::Loader
- Inherits:
-
Object
- Object
- Dry::System::Loader
- Defined in:
- lib/dry/system/loader.rb,
lib/dry/system/loader/autoloading.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.
Direct Known Subclasses
Defined Under Namespace
Classes: Autoloading
Class Method Summary collapse
-
.call(component, *args) ⇒ Object
Returns an instance of the component.
-
.constant(component) ⇒ Class
Returns the component’s class constant.
-
.require!(component) ⇒ Object
Requires the component’s source file.
Class Method Details
.call(component, *args) ⇒ Object
Returns an instance of the component
Provided optional args are passed to object’s constructor
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dry/system/loader.rb', line 46 def call(component, *args) require!(component) constant = self.constant(component) if singleton?(constant) constant.instance(*args) else constant.new(*args) end end |
.constant(component) ⇒ Class
Returns the component’s class constant
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dry/system/loader.rb', line 64 def constant(component) inflector = component.inflector const_name = inflector.camelize(component.const_path) inflector.constantize(const_name) rescue NameError => e # Ensure it's this component's constant, not any other NameError within the component if e. =~ /#{const_name}( |\n|$)/ raise ComponentNotLoadableError.new(component, e) else raise e end end |
.require!(component) ⇒ Object
Requires the component’s source file
32 33 34 35 |
# File 'lib/dry/system/loader.rb', line 32 def require!(component) require(component.require_path) self end |