Class: Dry::System::Config::Namespaces Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeNamespaces

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

#namespacesObject (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.

Examples:

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

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

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"

Parameters:

  • path (String)

    the path to the sub-directory of source files to which this namespace should apply, relative to the component dir

  • key (String, nil) (defaults to: path)

    the leading namespace to apply to the container keys for the components. Set ‘nil` for the keys to be top-level.

  • const (String, nil) (defaults to: path)

    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.

Returns:

Raises:

See Also:



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

See Also:



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

Parameters:

  • path (String)

    the path for the namespace

Returns:



125
126
127
# File 'lib/dry/system/config/namespaces.rb', line 125

def delete(path)
  namespaces.delete(path)
end

#delete_rootNamespace?

Deletes the configured root namespace and returns the namespace

If no root namespace was previously configured, returns nil

Returns:



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.

Yield Parameters:

  • namespace (Namespace)

    the yielded namespace



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

Returns:

  • (Boolean)


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

#lengthInteger Also known as: size

Returns the count of configured namespaces

Returns:

  • (Integer)


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

Returns:

  • (Namespace, nil)

    the namespace, if configured



36
37
38
# File 'lib/dry/system/config/namespaces.rb', line 36

def namespace(path)
  namespaces[path]
end

#pathsArray<String,nil>

Returns the paths of the configured namespaces

Returns:

  • (Array<String,nil>)

    the namespace paths, with nil representing the root namespace



146
147
148
# File 'lib/dry/system/config/namespaces.rb', line 146

def paths
  namespaces.keys
end

#rootNamespace?

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

Returns:

  • (Namespace, nil)

    the root namespace, if configured



47
48
49
# File 'lib/dry/system/config/namespaces.rb', line 47

def root
  namespaces[Namespace::ROOT_PATH]
end

#to_aArray<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.

Returns:



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