class Dry::System::Config::Namespaces

The configured namespaces for a ComponentDir

@see Config::ComponentDir#namespaces

@api private

Attributes

namespaces[R]

@api private

Public Class Methods

new() click to toggle source

@api private

# File lib/dry/system/config/namespaces.rb, line 20
def initialize
  @namespaces = {}
end

Public Instance Methods

[](path)
Alias for: namespace
add(path, key: path, const: path) click to toggle source

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.

@example Adding a namespace with top-level identifiers

# Components defined within admin/ (e.g. admin/my_component.rb) will be:
#
# - Registered with top-level identifiers ("my_component")
# - Expected to have constants in `Admin`, matching the namespace's path (Admin::MyComponent)

namespaces.add "admin", key: nil

@example Adding a namespace with top-level class constants

# Components defined within adapters/ (e.g. adapters/my_adapter.rb) will be:
#
# - Registered with leading identifiers matching the namespace's path ("adapters.my_adapter")
# - Expected to have top-level constants (::MyAdapter)

namespaces.add "adapters", const: nil

@example Adding a namespace with distinct identifiers and class constants

# Components defined within `bananas/` (e.g. bananas/banana_split.rb) will be:
#
# - Registered with the given leading identifier ("desserts.banana_split")
# - Expected to have constants within the given namespace (EatMe::Now::BananaSplit)

namespaces.add "bananas", key: "desserts", const: "eat_me/now"

@param path [String] the path to the sub-directory of source files to which this

namespace should apply, relative to the component dir

@param key [String, nil] the leading namespace to apply to the container keys

for the components. Set `nil` for the keys to be top-level.

@param const [String, nil] the Ruby constant namespace to expect for constants

defined within the components. This should be provided in underscored string
form, e.g. "hello_there/world" for a Ruby constant of `HelloThere::World`. Set
`nil` for the constants to be top-level.

@return [Namespace] the added namespace

@see Namespace

@api public

# File lib/dry/system/config/namespaces.rb, line 111
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) click to toggle source

Adds a root component dir namespace

@see add

@api public

# File lib/dry/system/config/namespaces.rb, line 124
def add_root(key: nil, const: nil)
  add(Namespace::ROOT_PATH, key: key, const: const)
end
delete(path) click to toggle source

Deletes the configured namespace for the given path and returns the namespace

If no namespace was previously configured for the given path, returns nil

@param path [String] the path for the namespace

@return [Namespace, nil]

@api public

# File lib/dry/system/config/namespaces.rb, line 137
def delete(path)
  namespaces.delete(path)
end
delete_root() click to toggle source

Deletes the configured root namespace and returns the namespace

If no root namespace was previously configured, returns nil

@return [Namespace, nil]

@api public

# File lib/dry/system/config/namespaces.rb, line 148
def delete_root
  delete(Namespace::ROOT_PATH)
end
each(&block) click to toggle source

Calls the given block once for each configured namespace, passing the namespace as an argument.

@yieldparam namespace [Namespace] the yielded namespace

@api public

# File lib/dry/system/config/namespaces.rb, line 202
def each(&block)
  to_a.each(&block)
end
empty?() click to toggle source

Returns true if there are no configured namespaces

@return [Boolean]

@api public

# File lib/dry/system/config/namespaces.rb, line 177
def empty?
  namespaces.empty?
end
initialize_copy(source) click to toggle source

@api private

Calls superclass method
# File lib/dry/system/config/namespaces.rb, line 25
def initialize_copy(source)
  super
  @namespaces = source.namespaces.dup
end
length() click to toggle source

Returns the count of configured namespaces

@return [Integer]

@api public

# File lib/dry/system/config/namespaces.rb, line 167
def length
  namespaces.length
end
Also aliased as: size
namespace(path) click to toggle source

Returns the namespace configured for the path, or nil if no such namespace has been configured

@return [Namespace, nil] the namespace, if configured

@api public

# File lib/dry/system/config/namespaces.rb, line 36
def namespace(path)
  namespaces[path]
end
Also aliased as: []
paths() click to toggle source

Returns the paths of the configured namespaces

@return [Array<String,nil>] the namespace paths, with nil representing the root

namespace

@api public

# File lib/dry/system/config/namespaces.rb, line 158
def paths
  namespaces.keys
end
root(**options) click to toggle source

Returns the namespace configured for the root path, or nil if the root namespace has not been configured

@return [Namespace, nil] the root namespace, if configured

@api public

# File lib/dry/system/config/namespaces.rb, line 47
def root(**options)
  if options.any?
    Dry::Core::Deprecations.announce(
      "Dry::System::Config::Namespaces#root (with arguments)",
      "Use `#add_root(key: nil, const: nil)` instead",
      tag: "dry-system",
      uplevel: 1
    )

    add_root(**options)
    return
  end

  namespaces[Namespace::ROOT_PATH]
end
size()
Alias for: length
to_a() click to toggle source

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.

@return [Array<Namespace>] the namespaces

@api public

# File lib/dry/system/config/namespaces.rb, line 190
def to_a
  namespaces.values.tap do |arr|
    arr << Namespace.default_root unless arr.any?(&:root?)
  end
end