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.
Class Method Summary collapse
-
.add(name, klass) ⇒ void
Register a device name.
-
.device_class(name) ⇒ Class?
Retrieve the class registered with the given name or nil if the name is not defined.
-
.new_device(name, options) ⇒ Lumberjack::Device
Instantiate a new device with the specified options from the device registry.
-
.registered?(name) ⇒ Boolean
Check if a device is registered.
-
.registered_devices ⇒ Hash
Return the map of registered device class names.
-
.remove(name) ⇒ void
Remove a device from the registry.
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.
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.
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.
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, ) 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, ) elsif klass == :stderr Device::Writer.new($stderr, ) else klass.new() end end |
.registered?(name) ⇒ Boolean
Check if a device is registered.
49 50 51 |
# File 'lib/lumberjack/device_registry.rb', line 49 def registered?(name) @registry.include?(name) end |
.registered_devices ⇒ Hash
Return the map of registered device class names.
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.
41 42 43 |
# File 'lib/lumberjack/device_registry.rb', line 41 def remove(name) @registry.delete(name) end |