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/RuntimeConfig.rb, line 23
23:   def initialize(appName, configFile = nil)
24:     @appName = appName
25:     @config = nil
26:     @debugMode = false
27: 
28:     if configFile
29:       # Read user specified config file.
30:       if File.exist?(configFile)
31:         begin
32:           @config = YAML::load(File.read(configFile))
33:         rescue
34:           $stderr.puts "Error in config file #{configFile}: #{$!}"
35:           exit 1
36:         end
37:       else
38:         $stderr.puts "Config file #{configFile} not found!"
39:         exit 1
40:       end
41:     else
42:       # Search config files in certain directories.
43:       @path = [ '.', ENV['HOME'], '/etc' ]
44: 
45:       @path.each do |path|
46:         # Try UNIX style hidden file first, then .rc.
47:         [ "#{path}/.#{appName}rc", "#{path}/#{appName}.rc" ].each do |file|
48:           if File.exist?(file)
49:             debug("Loading #{file}")
50:             @config = YAML::load(File.read(file))
51:             debug(@config.to_s)
52:             break
53:           end
54:         end
55:         break if @config
56:       end
57:     end
58:   end

Public Instance Methods

configure(object, section) click to toggle source
    # File lib/RuntimeConfig.rb, line 60
60:   def configure(object, section)
61:     debug("Configuring object of type #{object.class}")
62:     sections = section.split('.')
63:     p = @config
64:     sections.each do |sec|
65:       if p.nil? || !p.include?('_' + sec)
66:         debug("Section #{section} not found in config file")
67:         return false
68:       end
69:       p = p['_' + sec]
70:     end
71: 
72:     object.instance_variables.each do |iv|
73:       ivName = iv[1..1]
74:       debug("Processing class variable #{ivName}")
75:       if p.include?(ivName)
76:         debug("Setting @#{ivName} to #{p[ivName]}")
77:         object.instance_variable_set(iv, p[ivName])
78:       end
79:     end
80: 
81:     true
82:   end

Private Instance Methods

debug(message) click to toggle source
    # File lib/RuntimeConfig.rb, line 86
86:   def debug(message)
87:     return unless @debugMode
88: 
89:     puts message
90:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.