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.

Examples:


Lumberjack::FormatterRegistry.add(:upcase) { |value| value.to_s.upcase }
Lumberjack::FormatterRegistry.add(:currency, Lumberjack::Formatter::RoundFormatter, 2)

Lumberjack::EntryFormatter.build do |config|
  config.add_attribute :status, :upcase
  config.add_attribute :amount, :currency
end

Class Method Summary collapse

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.

Parameters:

  • name (Symbol)

    The name of the formatter

  • formatter (Class, #call) (defaults to: nil)

    The formatter or formatter class to register..

Raises:

  • (ArgumentError)


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.

Parameters:

  • name (Symbol)

    The name of the formatter

Returns:

  • (#call, nil)

    The registered formatter class or nil if not found



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.

Parameters:

  • name (Symbol)

    The name of the formatter

Returns:

  • (Boolean)

    True if the formatter is registered, false otherwise



55
56
57
# File 'lib/lumberjack/formatter_registry.rb', line 55

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

.registered_formattersHash

Return the map of registered formatters.

Returns:

  • (Hash)


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.

Parameters:

  • name (Symbol)

    The name of the formatter to remove



47
48
49
# File 'lib/lumberjack/formatter_registry.rb', line 47

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