Class: Dry::System::Component
- Inherits:
-
Object
- Object
- Dry::System::Component
- Defined in:
- lib/dry/system/component.rb
Overview
Components are objects providing information about auto-registered files. They expose an API to query this information and use a configurable loader object to initialize class instances.
Constant Summary collapse
Instance Attribute Summary collapse
- #file_path ⇒ Object readonly
- #identifier ⇒ Object readonly
- #namespace ⇒ Object readonly
- #options ⇒ Object readonly
Instance Method Summary collapse
- #auto_register? ⇒ Boolean private
-
#const_path ⇒ String
Returns an “underscored”, path-delimited representation of the component, appropriate for passing to the inflector for constantizing.
- #inflector ⇒ Object private
-
#initialize(identifier, file_path:, namespace:, **options) ⇒ Component
constructor
private
A new instance of Component.
-
#instance(*args) ⇒ Object
Returns the component’s instance.
-
#key ⇒ String
Returns the component’s unique key.
-
#loadable? ⇒ TrueClass
private
Returns true, indicating that the component is directly loadable from the files managed by the container.
- #loader ⇒ Object private
- #memoize? ⇒ Boolean private
-
#require_path ⇒ String
Returns a path-delimited representation of the compnent, appropriate for passing to ‘Kernel#require` to require its source file.
-
#root_key ⇒ Symbol
Returns the root namespace segment of the component’s key, as a symbol.
Constructor Details
#initialize(identifier, file_path:, namespace:, **options) ⇒ Component
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 Component.
40 41 42 43 44 45 |
# File 'lib/dry/system/component.rb', line 40 def initialize(identifier, file_path:, namespace:, **) @identifier = identifier @file_path = Pathname(file_path) @namespace = namespace @options = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
29 30 31 |
# File 'lib/dry/system/component.rb', line 29 def file_path @file_path end |
#identifier ⇒ Object (readonly)
25 26 27 |
# File 'lib/dry/system/component.rb', line 25 def identifier @identifier end |
#namespace ⇒ Object (readonly)
33 34 35 |
# File 'lib/dry/system/component.rb', line 33 def namespace @namespace end |
#options ⇒ Object (readonly)
37 38 39 |
# File 'lib/dry/system/component.rb', line 37 def @options end |
Instance Method Details
#auto_register? ⇒ Boolean
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.
160 161 162 |
# File 'lib/dry/system/component.rb', line 160 def auto_register? callable_option?([:auto_register]) end |
#const_path ⇒ String
Returns an “underscored”, path-delimited representation of the component, appropriate for passing to the inflector for constantizing
The const path takes into account the rules of the namespace used to load the component.
139 140 141 142 143 144 145 146 147 |
# File 'lib/dry/system/component.rb', line 139 def const_path namespace_const_path = namespace.const&.gsub(KEY_SEPARATOR, PATH_SEPARATOR) if namespace_const_path "#{namespace_const_path}#{PATH_SEPARATOR}#{path_in_namespace}" else path_in_namespace end end |
#inflector ⇒ Object
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.
155 156 157 |
# File 'lib/dry/system/component.rb', line 155 def inflector .fetch(:inflector) end |
#instance(*args) ⇒ Object
Returns the component’s instance
63 64 65 |
# File 'lib/dry/system/component.rb', line 63 def instance(*args) [:instance]&.call(self, *args) || loader.call(self, *args) end |
#key ⇒ String
Returns the component’s unique key
75 76 77 |
# File 'lib/dry/system/component.rb', line 75 def key identifier.key end |
#loadable? ⇒ TrueClass
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 true, indicating that the component is directly loadable from the files managed by the container
This is the inverse of IndirectComponent#loadable?
55 56 57 |
# File 'lib/dry/system/component.rb', line 55 def loadable? true end |
#loader ⇒ Object
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.
150 151 152 |
# File 'lib/dry/system/component.rb', line 150 def loader .fetch(:loader) end |
#memoize? ⇒ Boolean
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.
165 166 167 |
# File 'lib/dry/system/component.rb', line 165 def memoize? callable_option?([:memoize]) end |
#require_path ⇒ String
Returns a path-delimited representation of the compnent, appropriate for passing to ‘Kernel#require` to require its source file
The path takes into account the rules of the namespace used to load the component.
109 110 111 112 113 114 115 |
# File 'lib/dry/system/component.rb', line 109 def require_path if namespace.path "#{namespace.path}#{PATH_SEPARATOR}#{path_in_namespace}" else path_in_namespace end end |
#root_key ⇒ Symbol
Returns the root namespace segment of the component’s key, as a symbol
86 87 88 |
# File 'lib/dry/system/component.rb', line 86 def root_key identifier.root_key end |