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.
Components are created automatically through auto-registration and can be
accessed through Container.auto_register! which yields them.
Constant Summary collapse
- DEFAULT_OPTIONS =
{ separator: DEFAULT_SEPARATOR, namespace: nil }.freeze
Instance Attribute Summary collapse
- #file ⇒ Object readonly
- #identifier ⇒ Object readonly
- #loader ⇒ Object readonly
- #options ⇒ Object readonly
- #path ⇒ Object readonly
Class Method Summary collapse
- .cache ⇒ Object private
- .extract_identifier(name, ns, sep) ⇒ Object private
- .new(*args) ⇒ Object private
- .remove_namespace_from_path(name, ns) ⇒ Object private
Instance Method Summary collapse
- #auto_register? ⇒ Boolean private
- #boot_file(path) ⇒ Object private
- #bootable?(path) ⇒ Boolean private
- #file_exists?(paths) ⇒ Boolean private
-
#initialize(identifier, path, options) ⇒ Component
constructor
private
A new instance of Component.
-
#instance(*args) ⇒ Object
Returns components instance.
- #namespace ⇒ Object private
- #namespaced(namespace) ⇒ Object private
- #prepend(name) ⇒ Object private
- #root_key ⇒ Object private
- #separator ⇒ Object private
Constructor Details
#initialize(identifier, path, 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.
80 81 82 83 84 85 86 |
# File 'lib/dry/system/component.rb', line 80 def initialize(identifier, path, ) @identifier, @path = identifier, path = @file = "#{path}.rb".freeze @loader = .fetch(:loader) freeze end |
Instance Attribute Details
#file ⇒ Object (readonly)
33 34 35 |
# File 'lib/dry/system/component.rb', line 33 def file @file end |
#identifier ⇒ Object (readonly)
25 26 27 |
# File 'lib/dry/system/component.rb', line 25 def identifier @identifier end |
#loader ⇒ Object (readonly)
41 42 43 |
# File 'lib/dry/system/component.rb', line 41 def loader @loader end |
#options ⇒ Object (readonly)
37 38 39 |
# File 'lib/dry/system/component.rb', line 37 def end |
#path ⇒ Object (readonly)
29 30 31 |
# File 'lib/dry/system/component.rb', line 29 def path @path end |
Class Method Details
.cache ⇒ 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.
75 76 77 |
# File 'lib/dry/system/component.rb', line 75 def self.cache @cache ||= Concurrent::Map.new end |
.extract_identifier(name, ns, sep) ⇒ 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.
60 61 62 63 64 65 |
# File 'lib/dry/system/component.rb', line 60 def self.extract_identifier(name, ns, sep) name_s = name.to_s identifier = ns ? remove_namespace_from_path(name_s, ns) : name_s identifier.scan(WORD_REGEX).join(sep) end |
.new(*args) ⇒ 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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dry/system/component.rb', line 44 def self.new(*args) cache.fetch_or_store(args.hash) do name, = args = DEFAULT_OPTIONS.merge( || {}) ns, sep = .values_at(:namespace, :separator) identifier = extract_identifier(name, ns, sep) path = name.to_s.gsub(sep, PATH_SEPARATOR) loader = .fetch(:loader, Loader).new(path) super(identifier, path, .merge(loader: loader)) end end |
.remove_namespace_from_path(name, ns) ⇒ 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.
68 69 70 71 72 |
# File 'lib/dry/system/component.rb', line 68 def self.remove_namespace_from_path(name, ns) match_value = name.match(/^(?<remove_namespace>#{ns}).(?<identifier>.*)/) raise InvalidComponentError.new(name, "namespace +#{ns}+ not found in path") unless match_value match_value[:identifier] 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.
151 152 153 |
# File 'lib/dry/system/component.rb', line 151 def auto_register? !!.fetch(:auto_register) { true } end |
#boot_file(path) ⇒ 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.
117 118 119 |
# File 'lib/dry/system/component.rb', line 117 def boot_file(path) path.join("#{root_key}.rb") end |
#bootable?(path) ⇒ 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.
112 113 114 |
# File 'lib/dry/system/component.rb', line 112 def bootable?(path) boot_file(path).exist? end |
#file_exists?(paths) ⇒ 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.
122 123 124 |
# File 'lib/dry/system/component.rb', line 122 def file_exists?(paths) paths.any? { |path| path.join(file).exist? } end |
#instance(*args) ⇒ Object
Returns components instance
107 108 109 |
# File 'lib/dry/system/component.rb', line 107 def instance(*args) loader.call(*args) end |
#namespace ⇒ 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.
146 147 148 |
# File 'lib/dry/system/component.rb', line 146 def namespace [:namespace] end |
#namespaced(namespace) ⇒ 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.
134 135 136 137 138 |
# File 'lib/dry/system/component.rb', line 134 def namespaced(namespace) self.class.new( path, .merge(loader: loader.class, namespace: namespace) ) end |
#prepend(name) ⇒ 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.
127 128 129 130 131 |
# File 'lib/dry/system/component.rb', line 127 def prepend(name) self.class.new( [name, identifier].join(separator), .merge(loader: loader.class) ) end |
#root_key ⇒ 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.
156 157 158 |
# File 'lib/dry/system/component.rb', line 156 def root_key namespaces.first end |
#separator ⇒ 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.
141 142 143 |
# File 'lib/dry/system/component.rb', line 141 def separator [:separator] end |