Method: Faulty.register

Defined in:
lib/faulty.rb

.register(name, instance = nil, **config) {|Faulty::Options| ... } ⇒ Faulty?

Register an instance to the global Faulty state

Will not replace an existing instance with the same name. Check the return value if you need to know whether the instance already existed.

Parameters:

  • name (Symbol)

    The name of the instance to register

  • instance (Faulty) (defaults to: nil)

    The instance to register. If nil, a new instance will be created from the given options or block.

  • config (Hash)

    Attributes for Options

Yields:

Returns:

  • (Faulty, nil)

    The previously-registered instance of that name if it already existed, otherwise nil.

Raises:


86
87
88
89
90
91
92
93
94
95
96
# File 'lib/faulty.rb', line 86

def register(name, instance = nil, **config, &block)
  raise UninitializedError unless @instances

  if instance
    raise ArgumentError, 'Do not give config options if an instance is given' if !config.empty? || block
  else
    instance = new(**config, &block)
  end

  @instances.put_if_absent(name.to_s, instance)
end