Class: RuntimeConfig

Inherits:
Object show all
Defined in:
lib/taskjuggler/RuntimeConfig.rb

Overview

The RuntimeConfig searches for a YAML config file in a list of directories. When a file is found it is read-in. The read-in config values are grouped in a tree of sections. The values of a section can then be used to overwrite the instance variable of a passed object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appName, configFile = nil) ⇒ RuntimeConfig

Returns a new instance of RuntimeConfig.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/taskjuggler/RuntimeConfig.rb', line 24

def initialize(appName, configFile = nil)
  @appName = appName
  @config = nil
  @debugMode = false

  if configFile
    # Read user specified config file.
    unless loadConfigFile(configFile)
      error("Config file #{configFile} not found!")
    end
  else
    # Search config files in certain directories.
    [ '.', ENV['HOME'], '/etc' ].each do |path|
      # Try UNIX style hidden file first, then .rc.
      [ "#{path}/.#{appName}rc", "#{path}/#{appName}.rc" ].each do |file|
        break if loadConfigFile(file)
      end
    end
  end
end

Instance Attribute Details

#debugModeObject

Returns the value of attribute debugMode.



22
23
24
# File 'lib/taskjuggler/RuntimeConfig.rb', line 22

def debugMode
  @debugMode
end

Instance Method Details

#configure(object, section) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/taskjuggler/RuntimeConfig.rb', line 45

def configure(object, section)
  debug("Configuring object of type #{object.class}")
  sections = section.split('.')
  return false unless (p = @config)
  sections.each do |sec|
    p = p['_' + sec]
    unless p
      debug("Section #{section} not found in config file")
      return false
    end
  end

  object.instance_variables.each do |iv|
    ivName = iv[1..-1]
    debug("Processing class variable #{ivName}")
    if p.include?(ivName)
      debug("Setting @#{ivName} to #{p[ivName]}")
      object.instance_variable_set(iv, p[ivName])
    end
  end

  true
end