Class: Lumberjack::TemplateRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/template_registry.rb

Class Method Summary collapse

Class Method Details

.add(name, template) ⇒ Object

Register a log template class with a symbol.

Parameters:

  • name (Symbol)

    The name of the template.

  • template (String, Class, #call)

    The log template to register.



12
13
14
15
16
17
18
# File 'lib/lumberjack/template_registry.rb', line 12

def add(name, template)
  unless template.is_a?(String) || template.is_a?(Class) || template.respond_to?(:call)
    raise ArgumentError.new("template must be a String, Class, or respond to :call")
  end

  @templates[name.to_sym] = template
end

.registered?(name) ⇒ Boolean

Check if a template is registered.

Parameters:

  • name (Symbol)

    The name of the template.

Returns:

  • (Boolean)

    True if the template is registered, false otherwise.



32
33
34
# File 'lib/lumberjack/template_registry.rb', line 32

def registered?(name)
  @templates.include(name.to_sym)
end

.registered_templatesArray<Symbol>

List all registered log template symbols.

Returns:

  • (Array<Symbol>)

    An array of all registered log template symbols.



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

def registered_templates
  @templates.dup
end

.remove(name) ⇒ void

This method returns an undefined value.

Remove a template from the registry. raise ArgumentError.new(“template must be a String, Class, or respond to :call”)

Parameters:

  • name (Symbol)

    The name of the template to remove.



24
25
26
# File 'lib/lumberjack/template_registry.rb', line 24

def remove(name)
  @templates.delete(name.to_sym)
end

.template(name, options = {}) ⇒ Class?

Get a registered log template class by its symbol.

Parameters:

  • name (Symbol)

    The symbol of the registered log template class.

Returns:

  • (Class, nil)

    The registered log template class, or nil if not found.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lumberjack/template_registry.rb', line 40

def template(name, options = {})
  template = @templates[name.to_sym]
  if template.is_a?(Class)
    template.new(options)
  elsif template.is_a?(String)
    template_options = options.slice(:additional_lines, :time_format, :attribute_format, :colorize)
    Template.new(template, **template_options)
  else
    template
  end
end