Module: Lumberjack::DeviceRegistry

Defined in:
lib/lumberjack/device_registry.rb

Overview

The device registry is used for setting up names to represent Device classes. It is used in the constructor for Lumberjack::Logger and allows passing in a symbol to reference a device.

Devices must have a constructor that accepts the options hash as its sole argument in order to use the device registry.

The values :stdout and :stderr are registered by default and map to the standard output and standard error streams, respectively.

Examples:


Lumberjack::Device.register(:my_device, MyDevice)
logger = Lumberjack::Logger.new(:my_device)

Class Method Summary collapse

Class Method Details

.add(name, klass) ⇒ void

This method returns an undefined value.

Register a device name. Device names can be used to associate a symbol with a device class. The symbol can then be passed to Logger as the device argument.

Registered devices must take only one argument and that is the options hash for the device options.

Parameters:

  • name (Symbol)

    The name of the device

  • klass (Class)

    The device class to register

Raises:

  • (ArgumentError)


31
32
33
34
35
# File 'lib/lumberjack/device_registry.rb', line 31

def add(name, klass)
  raise ArgumentError.new("name must be a symbol") unless name.is_a?(Symbol)

  @registry[name] = klass
end

.device_class(name) ⇒ Class?

Retrieve the class registered with the given name or nil if the name is not defined.

Parameters:

  • name (Symbol)

    The name of the device

Returns:

  • (Class, nil)

    The registered device class or nil if not found



78
79
80
# File 'lib/lumberjack/device_registry.rb', line 78

def device_class(name)
  @registry[name]
end

.new_device(name, options) ⇒ Lumberjack::Device

Instantiate a new device with the specified options from the device registry.

Parameters:

  • name (Symbol)

    The name of the device

  • options (Hash)

    The device options

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lumberjack/device_registry.rb', line 58

def new_device(name, options)
  klass = device_class(name)
  unless klass
    valid_names = @registry.keys.map(&:inspect).join(", ")
    raise ArgumentError.new("#{name.inspect} is not registered as a device name; valid names are: #{valid_names}")
  end

  if klass == :stdout
    Device::Writer.new($stdout, options)
  elsif klass == :stderr
    Device::Writer.new($stderr, options)
  else
    klass.new(options)
  end
end

.registered?(name) ⇒ Boolean

Check if a device is registered.

Parameters:

  • name (Symbol)

    The name of the device

Returns:

  • (Boolean)

    True if the device is registered, false otherwise



49
50
51
# File 'lib/lumberjack/device_registry.rb', line 49

def registered?(name)
  @registry.include?(name)
end

.registered_devicesHash

Return the map of registered device class names.

Returns:

  • (Hash)


85
86
87
# File 'lib/lumberjack/device_registry.rb', line 85

def registered_devices
  @registry.dup
end

.remove(name) ⇒ void

This method returns an undefined value.

Remove a device from the registry.

Parameters:

  • name (Symbol)

    The name of the device to remove



41
42
43
# File 'lib/lumberjack/device_registry.rb', line 41

def remove(name)
  @registry.delete(name)
end