Created
February 26, 2011 05:41
-
-
Save manuelmeurer/844988 to your computer and use it in GitHub Desktop.
My app config setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in config/initializers/ | |
# prefixed with 'aaa_' so it's loaded first and app configs can be used in the following initializers | |
require 'ostruct' | |
config = File.read(Rails.root.join('config', 'app_config.yml')) | |
unless config.empty? | |
::AppConfig = OpenStruct.new(YAML.load(ERB.new(config).result).with_indifferent_access) | |
%w(development test production).each do |env| | |
if (env_config = AppConfig.delete_field(env)).present? && Rails.env == env | |
env_config.each do |k, v| | |
if AppConfig.send(k).present? | |
AppConfig.send(k).merge!(v) | |
else | |
AppConfig.send([k, '='].join, v) | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in config/ | |
# ERb can be used | |
s3: | |
key: devkey | |
secret: devsecret | |
foo: | |
bar: | |
baz: <%= 1.day %> | |
production: | |
s3: | |
key: realkey | |
secret: realkey |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# And this is how you use it in your code | |
AppConfig.foo[:bar][:baz] # 86400 (24*60*60) | |
AppConfig.s3[:key] # 'devkey' in development and test environment, 'realkey' in production environment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment