module Ric::Conf

Public Instance Methods

load_auto_conf(confname, opts={}) click to toggle source

This wants to be a magic configuration loader who looks for configuration automatically in many places, like:

  • ./.CONFNAME.yml

  • ~/.CONFNAME.yml

  • .CONFNAME/conf.yml

Loads a YAML file looked upon in common places and returns a hash with appropriate values, or an exception and maybe a nice explaination..

so you can call load_auto_conf('foo') and it will look throughout any ./.foo.yml, ~/.foo.yml or even /etc/foo.yml !

   # File lib/ric/conf.rb
20 def load_auto_conf(confname, opts={})
21   libver = '1.1'
22   dirs             = opts.fetch :dirs,          ['.', '~', '/etc/', '/etc/ric/auto_conf/']
23   file_patterns    = opts.fetch :file_patterns, [".#{confname}.yml", "#{confname}/conf.yml"]
24   sample_hash      = opts.fetch :sample_hash,   { 'load_auto_conf' => "please add an :sample_hash to me" , :anyway => "I'm in #{__FILE__}"}
25   verbose          = opts.fetch :verbose,       true
26   puts "load_auto_conf('#{confname}') v#{libver} start.." if verbose
27   dirs.each{|d|
28     dir = File.expand_path(d)
29     deb "DIR: #{dir}"
30     file_patterns.each{|fp|
31           # if YML exists return the load..
32       file = "#{dir}/#{fp}"
33       deb " - FILE: #{file}"
34       if File.exists?(file)
35         puts "Found! #{green file}"
36         yaml =  YAML.load( File.read(file) )
37         puts "load_auto_conf('#{confname}', v#{libver}) found: #{green yaml}"  if verbose
38         return yaml # in the future u can have a host based autoconf! Yay!
39       end
40     }
41   }
42   puts "Conf not found. Try this:\n---------------------------\n$ cat > ~/#{file_patterns.first}\n#{yellow "#Creatd by ric.rb:load_auto_conf()\n" +sample_hash.to_yaml}\n---------------------------\n"
43   raise "LoadAutoConf: configuration not found for '#{confname}'!"
44   return sample_hash
45 end