Parent

Class Index [+]

Quicksearch

RuntimeConfig

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.

Attributes

debugMode[RW]

Public Class Methods

new(appName, configFile = nil) click to toggle source
    # File lib/taskjuggler/RuntimeConfig.rb, line 24
24:   def initialize(appName, configFile = nil)
25:     @appName = appName
26:     @config = nil
27:     @debugMode = false
28: 
29:     if configFile
30:       # Read user specified config file.
31:       unless loadConfigFile(configFile)
32:         $stderr.puts "Config file #{configFile} not found!"
33:         exit 1
34:       end
35:     else
36:       # Search config files in certain directories.
37:       [ '.', ENV['HOME'], '/etc' ].each do |path|
38:         # Try UNIX style hidden file first, then .rc.
39:         [ "#{path}/.#{appName}rc", "#{path}/#{appName}.rc" ].each do |file|
40:           break if loadConfigFile(file)
41:         end
42:       end
43:     end
44:   end

Public Instance Methods

configure(object, section) click to toggle source
    # File lib/taskjuggler/RuntimeConfig.rb, line 46
46:   def configure(object, section)
47:     debug("Configuring object of type #{object.class}")
48:     sections = section.split('.')
49:     p = @config
50:     sections.each do |sec|
51:       if p.nil? || !p.include?('_' + sec)
52:         debug("Section #{section} not found in config file")
53:         return false
54:       end
55:       p = p['_' + sec]
56:     end
57: 
58:     object.instance_variables.each do |iv|
59:       ivName = iv[1..1]
60:       debug("Processing class variable #{ivName}")
61:       if p.include?(ivName)
62:         debug("Setting @#{ivName} to #{p[ivName]}")
63:         object.instance_variable_set(iv, p[ivName])
64:       end
65:     end
66: 
67:     true
68:   end

Private Instance Methods

debug(message) click to toggle source
    # File lib/taskjuggler/RuntimeConfig.rb, line 87
87:   def debug(message)
88:     return unless @debugMode
89: 
90:     puts message
91:   end
loadConfigFile(fileName) click to toggle source
    # File lib/taskjuggler/RuntimeConfig.rb, line 72
72:   def loadConfigFile(fileName)
73:     if File.exist?(fileName)
74:       begin
75:         debug("Loading #{fileName}")
76:         @config = YAML::load(File.read(fileName))
77:         debug(@config.to_s)
78:       rescue
79:         $stderr.puts "Error in config file #{fileName}: #{$!}"
80:         exit 1
81:       end
82:       return true
83:     end
84:     false
85:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.