Class: Dry::System::Config::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/system/config/namespace.rb

Overview

A configured namespace for a component dir

Namespaces consist of three elements:

  • The ‘path` within the component dir to which its namespace rules should apply.

  • A ‘key`, which determines the leading part of the key used to register each component in the container.

  • A ‘const`, which is the Ruby namespace expected to contain the class constants defined within each component’s source file. This value is expected to be an “underscored” string, intended to be run through the configured inflector to be converted into a real constant (e.g. ‘“foo_bar/baz”` will become `FooBar::Baz`)

Namespaces are added and configured for a component dir via Dry::System::Config::Namespaces#add.

Constant Summary collapse

ROOT_PATH =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, key:, const:) ⇒ Namespace

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 Namespace.



55
56
57
58
59
60
61
# File 'lib/dry/system/config/namespace.rb', line 55

def initialize(path:, key:, const:)
  @path = path
  # Default keys (i.e. when the user does not explicitly provide one) for non-root
  # paths will include path separators, which we must convert into key separators
  @key = key && key == path ? key.gsub(PATH_SEPARATOR, KEY_SEPARATOR) : key
  @const = const
end

Instance Attribute Details

#constObject (readonly)



37
38
39
# File 'lib/dry/system/config/namespace.rb', line 37

def const
  @const
end

#keyObject (readonly)



34
35
36
# File 'lib/dry/system/config/namespace.rb', line 34

def key
  @key
end

#pathObject (readonly)



31
32
33
# File 'lib/dry/system/config/namespace.rb', line 31

def path
  @path
end

Class Method Details

.default_rootNamespace

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 namespace configured to serve as the default root namespace for a component dir, ensuring that all code within the dir can be loaded, regardless of any other explictly configured namespaces

Returns:



46
47
48
49
50
51
52
# File 'lib/dry/system/config/namespace.rb', line 46

def self.default_root
  new(
    path: ROOT_PATH,
    key: nil,
    const: nil
  )
end

Instance Method Details

#path?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/dry/system/config/namespace.rb', line 69

def path?
  !root?
end

#root?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/dry/system/config/namespace.rb', line 64

def root?
  path == ROOT_PATH
end