Class: Dry::System::Config::Namespaces Private
- Inherits:
-
Object
- Object
- Dry::System::Config::Namespaces
- Defined in:
- lib/dry/system/config/namespaces.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The configured namespaces for a ComponentDir
Instance Attribute Summary collapse
- #namespaces ⇒ Object readonly private
Instance Method Summary collapse
-
#add(path, key: path, const: path) ⇒ Namespace
Adds a component dir namespace.
-
#add_root(key: nil, const: nil) ⇒ Object
Adds a root component dir namespace.
-
#delete(path) ⇒ Namespace?
Deletes the configured namespace for the given path and returns the namespace.
-
#delete_root ⇒ Namespace?
Deletes the configured root namespace and returns the namespace.
-
#each {|namespace| ... } ⇒ Object
Calls the given block once for each configured namespace, passing the namespace as an argument.
-
#empty? ⇒ Boolean
Returns true if there are no configured namespaces.
-
#initialize ⇒ Namespaces
constructor
private
A new instance of Namespaces.
- #initialize_copy(source) ⇒ Object private
-
#length ⇒ Integer
(also: #size)
Returns the count of configured namespaces.
-
#namespace(path) ⇒ Namespace?
(also: #[])
Returns the namespace configured for the path, or nil if no such namespace has been configured.
-
#paths ⇒ Array<String,nil>
Returns the paths of the configured namespaces.
-
#root ⇒ Namespace?
Returns the namespace configured for the root path, or nil if the root namespace has not been configured.
-
#to_a ⇒ Array<Namespace>
Returns the configured namespaces as an array.
Constructor Details
#initialize ⇒ Namespaces
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 Namespaces.
20 21 22 |
# File 'lib/dry/system/config/namespaces.rb', line 20 def initialize @namespaces = {} end |
Instance Attribute Details
#namespaces ⇒ Object (readonly)
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.
17 18 19 |
# File 'lib/dry/system/config/namespaces.rb', line 17 def namespaces @namespaces end |
Instance Method Details
#add(path, key: path, const: path) ⇒ Namespace
Adds a component dir namespace
A namespace encompasses a given sub-directory of the component dir, and determines (1) the leading segments of its components’ registered identifiers, and (2) the expected constant namespace of their class constants.
A namespace for a path can only be added once.
99 100 101 102 103 |
# File 'lib/dry/system/config/namespaces.rb', line 99 def add(path, key: path, const: path) raise NamespaceAlreadyAddedError, path if namespaces.key?(path) namespaces[path] = Namespace.new(path: path, key: key, const: const) end |
#add_root(key: nil, const: nil) ⇒ Object
Adds a root component dir namespace
112 113 114 |
# File 'lib/dry/system/config/namespaces.rb', line 112 def add_root(key: nil, const: nil) add(Namespace::ROOT_PATH, key: key, const: const) end |
#delete(path) ⇒ Namespace?
Deletes the configured namespace for the given path and returns the namespace
If no namespace was previously configured for the given path, returns nil
125 126 127 |
# File 'lib/dry/system/config/namespaces.rb', line 125 def delete(path) namespaces.delete(path) end |
#delete_root ⇒ Namespace?
Deletes the configured root namespace and returns the namespace
If no root namespace was previously configured, returns nil
136 137 138 |
# File 'lib/dry/system/config/namespaces.rb', line 136 def delete_root delete(Namespace::ROOT_PATH) end |
#each {|namespace| ... } ⇒ Object
Calls the given block once for each configured namespace, passing the namespace as an argument.
190 191 192 |
# File 'lib/dry/system/config/namespaces.rb', line 190 def each(&block) to_a.each(&block) end |
#empty? ⇒ Boolean
Returns true if there are no configured namespaces
165 166 167 |
# File 'lib/dry/system/config/namespaces.rb', line 165 def empty? namespaces.empty? end |
#initialize_copy(source) ⇒ 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.
25 26 27 28 |
# File 'lib/dry/system/config/namespaces.rb', line 25 def initialize_copy(source) super @namespaces = source.namespaces.dup end |
#length ⇒ Integer Also known as: size
Returns the count of configured namespaces
155 156 157 |
# File 'lib/dry/system/config/namespaces.rb', line 155 def length namespaces.length end |
#namespace(path) ⇒ Namespace? Also known as: []
Returns the namespace configured for the path, or nil if no such namespace has been configured
36 37 38 |
# File 'lib/dry/system/config/namespaces.rb', line 36 def namespace(path) namespaces[path] end |
#paths ⇒ Array<String,nil>
Returns the paths of the configured namespaces
146 147 148 |
# File 'lib/dry/system/config/namespaces.rb', line 146 def paths namespaces.keys end |
#root ⇒ Namespace?
Returns the namespace configured for the root path, or nil if the root namespace has not been configured
47 48 49 |
# File 'lib/dry/system/config/namespaces.rb', line 47 def root namespaces[Namespace::ROOT_PATH] end |
#to_a ⇒ Array<Namespace>
Returns the configured namespaces as an array
Adds a default root namespace to the end of the array if one was not added explicitly. This fallback ensures that all components in the component dir can be loaded.
178 179 180 181 182 |
# File 'lib/dry/system/config/namespaces.rb', line 178 def to_a namespaces.values.tap do |arr| arr << Namespace.default_root unless arr.any?(&:root?) end end |