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'       => []
}

Class Method Summary collapse

Class Method Details

.[](attr) ⇒ Object



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

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

.[]=(attr, val) ⇒ Object



60
61
62
# File 'lib/what/config.rb', line 60

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

.allObject



64
65
66
# File 'lib/what/config.rb', line 64

def self.all
  @config
end

.load(fn) ⇒ Object



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

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

.load_primary(fn) ⇒ Object



23
24
25
26
27
# File 'lib/what/config.rb', line 23

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



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

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)


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

def self.loaded?
  @loaded
end

.set_defaultsObject



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

def self.set_defaults
  @config = DEFAULTS
end