Module: Lumberjack::FormatterRegistry
- Defined in:
- lib/lumberjack/formatter_registry.rb
Overview
The formatter registry is used for setting up names to represent Formatter classes. It is used in the constructor for Lumberjack::Logger and allows passing in a symbol to reference a formatter.
Formatters must respond to the call method.
Class Method Summary collapse
-
.add(name, formatter = nil, &block) ⇒ void
Register a formatter name.
-
.formatter(name, *args) ⇒ #call?
Retrieve the formatter registered with the given name or nil if the name is not defined.
-
.registered?(name) ⇒ Boolean
Check if a formatter is registered.
-
.registered_formatters ⇒ Hash
Return the map of registered formatters.
-
.remove(name) ⇒ void
Remove a formatter from the registry.
Class Method Details
.add(name, formatter = nil, &block) ⇒ void
This method returns an undefined value.
Register a formatter name. Formatter names can be used to associate a symbol with a formatter class. The symbol can then be passed to Logger as the formatter argument.
Registered formatters must take only one argument and that is the options hash for the formatter options.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lumberjack/formatter_registry.rb', line 32 def add(name, formatter = nil, &block) raise ArgumentError.new("name must be a symbol") unless name.is_a?(Symbol) raise ArgumentError.new("formatter or block must be provided") if formatter.nil? && block.nil? raise ArgumentError.new("cannot have both formatter and a block") if !formatter.nil? && !block.nil? formatter ||= block raise ArgumentError.new("formatter must be a class or respond to call") unless formatter.is_a?(Class) || formatter.respond_to?(:call) @registry[name] = formatter end |
.formatter(name, *args) ⇒ #call?
Retrieve the formatter registered with the given name or nil if the name is not defined.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/lumberjack/formatter_registry.rb', line 63 def formatter(name, *args) instance = @registry[name] if instance.nil? valid_names = @registry.keys.map(&:inspect).join(", ") raise ArgumentError.new("#{name.inspect} is not registered as a formatter name; valid names are: #{valid_names}") end instance = instance.new(*args) if instance.is_a?(Class) instance end |
.registered?(name) ⇒ Boolean
Check if a formatter is registered.
55 56 57 |
# File 'lib/lumberjack/formatter_registry.rb', line 55 def registered?(name) @registry.include?(name) end |
.registered_formatters ⇒ Hash
Return the map of registered formatters.
79 80 81 |
# File 'lib/lumberjack/formatter_registry.rb', line 79 def registered_formatters @registry.dup end |
.remove(name) ⇒ void
This method returns an undefined value.
Remove a formatter from the registry.
47 48 49 |
# File 'lib/lumberjack/formatter_registry.rb', line 47 def remove(name) @registry.delete(name) end |