class Dry::System::Config::ComponentDirs

Public Class Methods

new() click to toggle source

@api private

# File lib/dry/system/config/component_dirs.rb, line 88
def initialize
  @dirs = Concurrent::Map.new
end

Public Instance Methods

add(path) { |dir| ... } click to toggle source

Adds and configures a component dir

@param path [String] the path for the component dir, relative to the configured

container root

@yieldparam dir [ComponentDir] the component dir to configure

@return [ComponentDir] the added component dir

@example

component_dirs.add "lib" do |dir|
  dir.default_namespace = "my_app"
end

@see ComponentDir

# File lib/dry/system/config/component_dirs.rb, line 113
def add(path)
  raise ComponentDirAlreadyAddedError, path if dirs.key?(path)

  dirs[path] = ComponentDir.new(path).tap do |dir|
    apply_defaults_to_dir(dir)
    yield dir if block_given?
  end
end
dirs() click to toggle source

Returns the added component dirs, with default settings applied

@return [Hash<String, ComponentDir>] the component dirs as a hash, keyed by path

# File lib/dry/system/config/component_dirs.rb, line 125
def dirs
  @dirs.each { |_, dir| apply_defaults_to_dir(dir) }
end
each(&block) click to toggle source

Calls the given block once for each added component dir, passing the dir as an argument.

@yieldparam dir [ComponentDir] the yielded component dir

# File lib/dry/system/config/component_dirs.rb, line 140
def each(&block)
  to_a.each(&block)
end
initialize_copy(source) click to toggle source

@api private

Calls superclass method
# File lib/dry/system/config/component_dirs.rb, line 93
def initialize_copy(source)
  super
  @dirs = source.dirs.dup
end
to_a() click to toggle source

Returns the added component dirs, with default settings applied

@return [Array<ComponentDir>]

# File lib/dry/system/config/component_dirs.rb, line 132
def to_a
  dirs.values
end

Private Instance Methods

apply_defaults_to_dir(dir) click to toggle source

Apply default settings to a component dir. This is run every time the dirs are accessed to ensure defaults are applied regardless of when new component dirs are added. This method must be idempotent.

@return [void]

# File lib/dry/system/config/component_dirs.rb, line 151
def apply_defaults_to_dir(dir)
  dir.config.values.each do |key, _value|
    if configured?(key) && !dir.configured?(key)
      dir.public_send(:"#{key}=", public_send(key))
    end
  end
end
configured?(key) click to toggle source

Returns true if a setting has been explicitly configured and is not returning just a default value.

This is used to determine which settings should be applied to added component dirs as additional defaults.

@api private

# File lib/dry/system/config/component_dirs.rb, line 166
def configured?(key)
  config._settings[key].input_defined?
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/dry/system/config/component_dirs.rb, line 170
def method_missing(name, *args, &block)
  if config.respond_to?(name)
    config.public_send(name, *args, &block)
  else
    super
  end
end
respond_to_missing?(name, include_all = false) click to toggle source
Calls superclass method
# File lib/dry/system/config/component_dirs.rb, line 178
def respond_to_missing?(name, include_all = false)
  config.respond_to?(name) || super
end