Class: What::Config

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

Constant Summary collapse

DEFAULTS =
{
  'interval'              => 10,
  'formatter'             => 'json',
  'configs'               => [],
  'config_fragment_dir'   => '/etc/what/conf.d',
  'module_paths'          => [],
  'modules'               => []
}

Class Method Summary collapse

Class Method Details

.[](attr) ⇒ Object



58
59
60
# File 'lib/what/config.rb', line 58

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

.[]=(attr, val) ⇒ Object



62
63
64
# File 'lib/what/config.rb', line 62

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

.allObject



66
67
68
# File 'lib/what/config.rb', line 66

def self.all
  @config
end

.load(fn) ⇒ Object



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

def self.load(fn)
  set_defaults
  load_primary(fn)
  load_secondary(@config['configs'])
  load_secondary(Dir.glob("#{@config['config_fragment_dir']}/*.yml"))
end

.load_primary(fn) ⇒ Object



25
26
27
28
29
# File 'lib/what/config.rb', line 25

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

.load_secondary(fns) ⇒ Object



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

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)

      # Special-case modules--we want to append, not overwrite
      @config['modules'] ||= []
      @config['modules'] += Array(opts.delete('modules'))

      @config.merge!(opts)
    rescue Exception => e
      puts "Error loading config file #{path}: #{e}"
    end
  end
end

.loaded?Boolean

Returns:

  • (Boolean)


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

def self.loaded?
  @loaded
end

.set_defaultsObject



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

def self.set_defaults
  @config = DEFAULTS
end