Method: Dry::System::Config::ComponentDir#instance=

Defined in:
lib/dry/system/config/component_dir.rb

#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