public
Description: Provides a way to manage environment specific configuration settings.
Homepage:
Clone URL: git://github.com/UnderpantsGnome/config_reader-gem.git
Mon Dec 08 16:01:03 -0800 2008
commit  0902cb879672145e819a3fd320e2105359d61a5a
tree    50bee60cde8bf689143b6046c2b5f35e7ba13172
parent  cc2975e41a08821b1ff4e3ec44c9d80f4ce7f8f4
config_reader-gem / lib / config_reader.rb
cc2975e4 » UnderpantsGnome 2008-08-06 Initial commit 1 require 'yaml'
2 begin
3 require 'erb'
4 rescue LoadError
5 puts "ERB not found, you won't be able to use ERB in your config"
6 end
7
8 $:.unshift(File.dirname(__FILE__)) unless
9 $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
10
11 class ConfigReader
12 @config_file = nil
13 @config = nil
14
15 class << self
16 def config
17 @config = nil unless defined?(@config)
18 @config ||= reload
19 end
20
21 def config_file=(file)
22 @config_file = file
23 end
24
25 def reload
26 raise 'No config file set' unless @config_file
27
0902cb87 » UnderpantsGnome 2008-12-08 fix the environment merge f... 28 if defined?(ERB)
cc2975e4 » UnderpantsGnome 2008-08-06 Initial commit 29 conf = YAML.load(ERB.new(File.open(find_config).read).result)
30 else
31 conf = YAML.load(File.open(find_config).read)
32 end
33
34 raise 'No config found' unless conf
35
36 if defined?(RAILS_ENV)
37 env = RAILS_ENV
38 elsif defined?(APP_ENV)
39 env = APP_ENV
40 end
41
42 _conf = conf['defaults']
0902cb87 » UnderpantsGnome 2008-12-08 fix the environment merge f... 43 _conf.merge!(conf[env]) if conf[env]
cc2975e4 » UnderpantsGnome 2008-08-06 Initial commit 44 _conf
45 end
46
47 def [](key)
48 config[key]
49 end
0902cb87 » UnderpantsGnome 2008-12-08 fix the environment merge f... 50
cc2975e4 » UnderpantsGnome 2008-08-06 Initial commit 51 def find_config
0902cb87 » UnderpantsGnome 2008-12-08 fix the environment merge f... 52 return @config_file if File.exist?(@config_file)
53
cc2975e4 » UnderpantsGnome 2008-08-06 Initial commit 54 %w( . config ).each do |dir|
55 config_file = File.join(dir, @config_file)
56 return config_file if File.exist?(config_file)
57 end
58 ''
0902cb87 » UnderpantsGnome 2008-12-08 fix the environment merge f... 59 end
cc2975e4 » UnderpantsGnome 2008-08-06 Initial commit 60
61 def method_missing(key)
62 config[key.to_s] || super
63 end
0902cb87 » UnderpantsGnome 2008-12-08 fix the environment merge f... 64
cc2975e4 » UnderpantsGnome 2008-08-06 Initial commit 65 def inspect
66 puts config.inspect
67 end
68 end
69 end
70
71 class Hash
72 def method_missing(key)
73 self[key.to_s] || super
74 end
75 end