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.
# File lib/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: if File.exist?(configFile) 32: begin 33: @config = YAML::load(File.read(configFile)) 34: rescue 35: $stderr.puts "Error in config file #{configFile}: #{$!}" 36: exit 1 37: end 38: else 39: $stderr.puts "Config file #{configFile} not found!" 40: exit 1 41: end 42: else 43: # Search config files in certain directories. 44: @path = [ '.', ENV['HOME'], '/etc' ] 45: 46: @path.each do |path| 47: # Try UNIX style hidden file first, then .rc. 48: [ "#{path}/.#{appName}rc", "#{path}/#{appName}.rc" ].each do |file| 49: if File.exist?(file) 50: debug("Loading #{file}") 51: @config = YAML::load(File.read(file)) 52: debug(@config.to_s) 53: break 54: end 55: end 56: break if @config 57: end 58: end 59: end
# File lib/RuntimeConfig.rb, line 61 61: def configure(object, section) 62: debug("Configuring object of type #{object.class}") 63: sections = section.split('.') 64: p = @config 65: sections.each do |sec| 66: if p.nil? || !p.include?('_' + sec) 67: debug("Section #{section} not found in config file") 68: return false 69: end 70: p = p['_' + sec] 71: end 72: 73: object.instance_variables.each do |iv| 74: ivName = iv[1..1] 75: debug("Processing class variable #{ivName}") 76: if p.include?(ivName) 77: debug("Setting @#{ivName} to #{p[ivName]}") 78: object.instance_variable_set(iv, p[ivName]) 79: end 80: end 81: 82: true 83: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.