Class: NagiosHerald::FormatterLoader

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/nagios-herald/formatter_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#configure_logger_for, #logger, #logger_for

Constructor Details

#initializeFormatterLoader

Public: Initialize the formatter loader module. Adds the path to the built-in formatters to the module variable @formatter_paths. Adds the path to custom formatters if that option is passed to ‘nagios-herald` via ’-F|–formatter-dir’ on the command line or via the ‘formatter-dir’ option in the configuration file.



21
22
23
24
# File 'lib/nagios-herald/formatter_loader.rb', line 21

def initialize
  @builtin_formatter_path = File.expand_path("formatters", File.dirname(__FILE__))
  @custom_formatter_path = Config.config['formatter_dir'] if Config.config['formatter_dir']
end

Instance Attribute Details

#builtin_formatter_pathObject

Returns the value of attribute builtin_formatter_path.



13
14
15
# File 'lib/nagios-herald/formatter_loader.rb', line 13

def builtin_formatter_path
  @builtin_formatter_path
end

#custom_formatter_pathObject

Returns the value of attribute custom_formatter_path.



14
15
16
# File 'lib/nagios-herald/formatter_loader.rb', line 14

def custom_formatter_path
  @custom_formatter_path
end

Instance Method Details

#enum_formatter_class_filesObject

Public: Enumerate the available formatter class files.

Returns a hash of the class files’ absolute paths whose key is the formatter file’s basename and the value is the full path to the file.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nagios-herald/formatter_loader.rb', line 31

def enum_formatter_class_files
  formatter_class_files = {}

  # Iterate over the builtin formatters first.
  builtin_formatter_class_files = Dir.glob(File.expand_path("*.rb", @builtin_formatter_path))
  builtin_formatter_class_files.each do |builtin_formatter_class_file|
    formatter_class_files[File.basename(builtin_formatter_class_file)] = builtin_formatter_class_file
  end
  # If we've been told about custom formatters, add them to the hash.
  # If there are conflicts, naively merge them, with the custom formatters getting
  # priority.
  if @custom_formatter_path
    custom_formatters = {}
    custom_formatter_class_files = Dir.glob(File.expand_path("*.rb", @custom_formatter_path))
    custom_formatter_class_files.each do |custom_formatter_class_file|
      custom_formatters[File.basename(custom_formatter_class_file)] = custom_formatter_class_file
    end
    formatter_class_files.merge!(custom_formatters)
  end
  formatter_class_files
end

#formatter_class_filesObject

Public: An array of class files’ paths.

Returns an array of class files’ paths.



56
57
58
# File 'lib/nagios-herald/formatter_loader.rb', line 56

def formatter_class_files
  @formatter_class_files ||= enum_formatter_class_files
end

#load_formattersObject

Public: Load the formatters into the namespace. A formatter can then easily be instantiated later.

Returns nothing but loads the classes into the namespace.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nagios-herald/formatter_loader.rb', line 64

def load_formatters
  if formatter_class_files.empty?
    if @custom_formatter_path
      puts "#{$0}: No formatters were found in '#{@builtin_formatter_path}'" \
      " or '#{@custom_formatter_path}' (as defined by the 'formatter_dir' option)!"
    else
      puts "#{$0}: No formatters were found in '#{@builtin_formatter_path}'!"
    end
    exit 1
  else
    formatter_class_files.each do |formatter_class, formatter_class_file|
      Kernel.load formatter_class_file
    end
  end
end