Class: What::Config

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

Constant Summary collapse

DEFAULTS =
{
  'interval'      => 10,
  'formatter'     => 'json',
  'configs'       => [],
  'module_paths'  => [],
  'modules'       => [],
  'module_config' => {}
}

Class Method Summary collapse

Class Method Details

.[](attr) ⇒ Object



47
48
49
# File 'lib/what/config.rb', line 47

def self.[](attr)
  @config[attr]
end

.[]=(attr, val) ⇒ Object



51
52
53
# File 'lib/what/config.rb', line 51

def self.[]=(attr, val)
  @config[attr] = val
end

.allObject



55
56
57
# File 'lib/what/config.rb', line 55

def self.all
  @config
end

.load(fn) ⇒ Object



14
15
16
17
# File 'lib/what/config.rb', line 14

def self.load(fn)
  load_primary(fn)
  load_secondary(@config['configs'])
end

.load_primary(fn) ⇒ Object



19
20
21
22
23
# File 'lib/what/config.rb', line 19

def self.load_primary(fn)
  @config = DEFAULTS.merge(YAML.load_file(fn))
  @config['base'] ||= File.expand_path(File.dirname(fn))
  @loaded = true
end

.load_secondary(fns) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/what/config.rb', line 25

def self.load_secondary(fns)
  return if !fns

  fns.each do |fn|
    path = if fn.match(%r(^/))
             fn
           else
             File.join(@config['base'], fn)
           end
    begin
      opts = YAML.load_file(path)
      @config.merge!(opts)
    rescue Exception => e
      puts "Error loading config file #{path}: #{e}"
    end
  end
end

.loaded?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/what/config.rb', line 43

def self.loaded?
  @loaded
end