Created
September 26, 2012 21:33
-
-
Save coreyti/3790731 to your computer and use it in GitHub Desktop.
Load settings for Rails from YAML files
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
require File.expand_path('../boot', __FILE__) # etc. | |
module Example | |
class Application < Rails::Application | |
# Helper for loading deeply nested environment config. | |
def load_env(context, key, value) | |
if value.is_a?(Hash) | |
options = begin | |
context.send(key) || ActiveSupport::OrderedOptions.new | |
rescue | |
ActiveSupport::OrderedOptions.new | |
end | |
context.send("#{key}=", options) | |
value.each do |k, v| | |
load_env(options, k, v) | |
end | |
else | |
context.send("#{key}=", value) | |
end | |
end | |
# Load environment settings from yaml files. | |
["config/environment.yml", "config/environments/#{Rails.env}.yml"].each do |path| | |
yml = YAML.load_file(Rails.root.join(path)) | |
yml && yml.each do |key, value| | |
load_env(config, key, value) | |
end | |
end | |
# more config... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment