Class: Dry::System::Config::ComponentDir

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/dry/system/config/component_dir.rb

Instance Attribute Summary collapse

Settings collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) {|_self| ... } ⇒ ComponentDir

Returns a new instance of ComponentDir.

Yields:

  • (_self)

Yield Parameters:



201
202
203
204
205
# File 'lib/dry/system/config/component_dir.rb', line 201

def initialize(path)
  super()
  @path = path
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



214
215
216
217
218
219
220
# File 'lib/dry/system/config/component_dir.rb', line 214

def method_missing(name, *args, &block)
  if config.respond_to?(name)
    config.public_send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#pathString (readonly)

Returns the component dir path, relative to the configured container root

Returns:

  • (String)

    the path



198
199
200
# File 'lib/dry/system/config/component_dir.rb', line 198

def path
  @path
end

Instance Method Details

#add_to_load_pathBoolean

Returns the configured value.

Returns:

  • (Boolean)

See Also:



191
# File 'lib/dry/system/config/component_dir.rb', line 191

setting :add_to_load_path, default: true

#add_to_load_path=(policy) ⇒ Boolean

Sets whether the dir should be added to the ‘$LOAD_PATH` after the container is configured.

Defaults to ‘true`. This may need to be set to `false` when using a class autoloading system.

Parameters:

  • policy (Boolean)

Returns:

  • (Boolean)

See Also:



191
# File 'lib/dry/system/config/component_dir.rb', line 191

setting :add_to_load_path, default: true

#auto_registerBoolean, Proc

Returns the configured auto-registration policy.

Returns:

  • (Boolean, Proc)

    the configured policy

See Also:



47
# File 'lib/dry/system/config/component_dir.rb', line 47

setting :auto_register, default: true

#auto_register=(policy) ⇒ Boolean, Proc

Sets the auto-registration policy for the component dir.

This may be a simple boolean to enable or disable auto-registration for all components, or a proc accepting a Dry::System::Component and returning a boolean to configure auto-registration on a per-component basis

Defaults to ‘true`.

Examples:

dir.auto_register = false
dir.auto_register = proc do |component|
  !component.identifier.start_with?("entities")
end

Parameters:

  • policy (Boolean, Proc)

Returns:

  • (Boolean, Proc)

See Also:



47
# File 'lib/dry/system/config/component_dir.rb', line 47

setting :auto_register, default: true

#auto_register?Boolean

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:

  • (Boolean)


208
209
210
# File 'lib/dry/system/config/component_dir.rb', line 208

def auto_register?
  !!config.auto_register
end

#instanceProc?

Returns the configured instance proc.

Returns:

  • (Proc, nil)

See Also:



90
# File 'lib/dry/system/config/component_dir.rb', line 90

setting :instance

#instance=(instance_proc) ⇒ Proc

Sets a proc used to return the instance of any component within the component dir.

This proc should accept a Dry::System::Component and return the object to serve as the component’s instance.

When you provide an instance proc, it will be used in preference to the #loader (either the default loader or an explicitly configured one). Provide an instance proc when you want a simple way to customize the instance for certain components. For complete control, provide a replacement loader via #loader=.

Defaults to ‘nil`.

Examples:

dir.instance = proc do |component|
  if component.key.match?(/workers\./)
    # Register classes for jobs
    component.loader.constant(component)
  else
    # Otherwise register regular instances per default loader
    component.loader.call(component)
  end
end

Parameters:

  • instance_proc (Proc, nil)

Returns:

  • (Proc)

See Also:

  • Loader


90
# File 'lib/dry/system/config/component_dir.rb', line 90

setting :instance

#loader#call

Returns the configured loader.

Returns:

  • (#call)

See Also:



118
# File 'lib/dry/system/config/component_dir.rb', line 118

setting :loader, default: Dry::System::Loader

#loader=(loader) ⇒ #call

Sets the loader to use when registering components from the dir in the container.

Defaults to ‘Dry::System::Loader`.

When using an autoloader like Zeitwerk, consider using ‘Dry::System::Loader::Autoloading`

Parameters:

  • loader (#call)

    the loader

Returns:

  • (#call)

    the configured loader

See Also:



118
# File 'lib/dry/system/config/component_dir.rb', line 118

setting :loader, default: Dry::System::Loader

#memoizeBoolean, Proc

Returns the configured memoization policy.

Returns:

  • (Boolean, Proc)

    the configured memoization policy

See Also:



154
# File 'lib/dry/system/config/component_dir.rb', line 154

setting :memoize, default: false

#memoize=(policy) ⇒ Boolean, Proc

Sets whether to memoize components from the dir when registered in the container.

This may be a simple boolean to enable or disable memoization for all components, or a proc accepting a ‘Dry::Sytem::Component` and returning a boolean to configure memoization on a per-component basis

Defaults to ‘false`.

Examples:

dir.memoize = true
dir.memoize = proc do |component|
  !component.identifier.start_with?("providers")
end

Parameters:

  • policy (Boolean, Proc)

Returns:

  • (Boolean, Proc)

    the configured memoization policy

See Also:



154
# File 'lib/dry/system/config/component_dir.rb', line 154

setting :memoize, default: false

#namespacesNamespaces

Returns the configured namespaces for the component dir.

Allows namespaces to added on the returned object via Namespaces#add.

Returns:

See Also:



166
# File 'lib/dry/system/config/component_dir.rb', line 166

setting :namespaces, default: Namespaces.new, cloneable: true