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, **kwargs) ⇒ 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.
159 160 161 |
# File 'lib/dry/system/component.rb', line 159 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.
138 139 140 141 142 143 144 145 146 |
# File 'lib/dry/system/component.rb', line 138 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.
154 155 156 |
# File 'lib/dry/system/component.rb', line 154 def inflector .fetch(:inflector) end |
#instance(*args, **kwargs) ⇒ Object
Returns the component’s instance
63 64 65 |
# File 'lib/dry/system/component.rb', line 63 def instance(*args, **kwargs) [:instance]&.call(self, *args, **kwargs) || loader.call(self, *args, **kwargs) end |
#key ⇒ String
Returns the component’s unique key
74 75 76 |
# File 'lib/dry/system/component.rb', line 74 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.
149 150 151 |
# File 'lib/dry/system/component.rb', line 149 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.
164 165 166 |
# File 'lib/dry/system/component.rb', line 164 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.
108 109 110 111 112 113 114 |
# File 'lib/dry/system/component.rb', line 108 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
85 86 87 |
# File 'lib/dry/system/component.rb', line 85 def root_key identifier.root_key end |