Class: ApacheCrunch::FormatDefinitionFinder

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

Overview

Finds a named log format string in the configuration file(s)

Constant Summary collapse

@@FILE_NAME =
"log_formats.rb"
@@DEFAULT_FORMATS =
{
    :ncsa => %q!%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"!,
    :ubuntu => %q!%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"!
}

Instance Method Summary collapse

Constructor Details

#initialize(file_cls = File, env = ENV) ⇒ FormatDefinitionFinder

Initializes the FormatStringFinder.



11
12
13
14
# File 'lib/config.rb', line 11

def initialize(file_cls=File, env=ENV)
    @_file_cls=file_cls
    @_env=env
end

Instance Method Details

#_search_pathObject



38
39
40
41
42
# File 'lib/config.rb', line 38

def _search_path
    [".", "./etc",
     @_file_cls.join(@_env["HOME"], ".apachecrunch"),
     "/etc/apachecrunch"]
end

#find(format_name) ⇒ Object

Finds the given format string in the configuration file(s)

If none exists, returns nil.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/config.rb', line 19

def find(format_name)
    name_as_symbol = format_name.to_sym

    formats = @@DEFAULT_FORMATS.clone
    _search_path.each do |dir|
        config_path = @_file_cls.join(dir, @@FILE_NAME)
        if @_file_cls.readable?(config_path)
            config_file = @_file_cls.open(@_file_cls.join(dir, @@FILE_NAME))
            eval config_file.read
        end

        if formats.key?(format_name.to_sym)
            return formats[format_name.to_sym].gsub(/\\"/, '"')
        end
    end

    raise "Failed to find the format '#{format_name}' in the search path: #{_search_path.inspect}"
end