Sha256: 312305927ec277edfd6fef858c8422748f77265b37b7cf5c82900ce719386f40

Contents?: true

Size: 1.82 KB

Versions: 16

Compression:

Stored size: 1.82 KB

Contents

module Ric
 module Conf
   
   require 'yaml'

=begin
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 !

=end

   def load_auto_conf(confname, opts={})
     libver = '1.1'
     dirs             = opts.fetch :dirs,          ['.', '~', '/etc/', '/etc/ric/auto_conf/']
     file_patterns    = opts.fetch :file_patterns, [".#{confname}.yml", "#{confname}/conf.yml"]
     sample_hash      = opts.fetch :sample_hash,   { 'load_auto_conf' => "please add an :sample_hash to me" , :anyway => "I'm in #{__FILE__}"}
     verbose          = opts.fetch :verbose,       true
     puts "load_auto_conf('#{confname}') v#{libver} start.." if verbose
     dirs.each{|d|
       dir = File.expand_path(d)
       deb "DIR: #{dir}"
       file_patterns.each{|fp|
             # if YML exists return the load..
         file = "#{dir}/#{fp}"
         deb " - FILE: #{file}"
         if File.exists?(file)
           puts "Found! #{green file}"
           yaml =  YAML.load( File.read(file) )
           puts "load_auto_conf('#{confname}', v#{libver}) found: #{green yaml}"  if verbose
           return yaml # in the future u can have a host based autoconf! Yay!
         end
       }
     }
     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"
     raise "LoadAutoConf: configuration not found for '#{confname}'!"
     return sample_hash
   end

   
 end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ric-0.14.2 lib/ric/conf.rb
ric-0.14.1 lib/ric/conf.rb
ric-0.14.0 lib/ric/conf.rb
ric-0.13.0 lib/ric/conf.rb
ric-0.12.2 lib/ric/conf.rb
ric-0.12.0 lib/ric/conf.rb
ric-0.11.19 lib/ric/conf.rb
ric-0.11.18 lib/ric/conf.rb
ric-0.11.16 lib/ric/conf.rb
ric-0.11.15 lib/ric/conf.rb
ric-0.11.14 lib/ric/conf.rb
ric-0.11.13 lib/ric/conf.rb
ric-0.11.12 lib/ric/conf.rb
ric-0.11.11 lib/ric/conf.rb
ric-0.11.10 lib/ric/conf.rb
ric-0.11.9 lib/ric/conf.rb