module RequestLogAnalyzer::Output

Module for generating output

Public Class Methods

load(file_format, *args) click to toggle source

Loads a Output::Base subclass instance.

   # File lib/request_log_analyzer/output.rb
 4 def self.load(file_format, *args)
 5   klass = nil
 6   if file_format.is_a?(RequestLogAnalyzer::Output::Base)
 7     # this already is a file format! return itself
 8     return file_format
 9 
10   elsif file_format.is_a?(Class) && file_format.ancestors.include?(RequestLogAnalyzer::Output::Base)
11     # a usable class is provided. Use this format class.
12     klass = file_format
13 
14   elsif file_format.is_a?(String) && File.exist?(file_format)
15     # load a format from a ruby file
16     require file_format
17     const = RequestLogAnalyzer.to_camelcase(File.basename(file_format, '.rb'))
18     if RequestLogAnalyzer::FileFormat.const_defined?(const)
19       klass = RequestLogAnalyzer::Output.const_get(const)
20     elsif Object.const_defined?(const)
21       klass = Object.const_get(const)
22     else
23       fail "Cannot load class #{const} from #{file_format}!"
24     end
25 
26   else
27     # load a provided file format
28     klass = RequestLogAnalyzer::Output.const_get(RequestLogAnalyzer.to_camelcase(file_format))
29   end
30 
31   # check the returned klass to see if it can be used
32   fail "Could not load a file format from #{file_format.inspect}" if klass.nil?
33   fail 'Invalid FileFormat class' unless klass.is_a?(Class) && klass.ancestors.include?(RequestLogAnalyzer::Output::Base)
34 
35   klass.create(*args) # return an instance of the class
36 end