Created
October 31, 2009 01:42
-
-
Save stevegraham/222878 to your computer and use it in GitHub Desktop.
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
class AppConfig | |
# Loads a YAML configuration file from RAILS_ROOT/config/. The default file | |
# it looks for is 'application.yml', although if this doesn't match your | |
# application, you can pass in an alternative value as an argument | |
# to AppConfig.load. | |
# After the file has been loaded, any inline ERB is evaluated and unserialized | |
# into a hash. For each key-value pair in the hash, class getter and setter methods | |
# are defined i.e., AppConfig.key => "value" | |
# This allows you to store your application configuration information e.g., API keys and | |
# authentication credentials in a convenient manner, external to your application source | |
# | |
# application.yml example | |
# | |
# :defaults: &defaults | |
# :app_name: Platform | |
# :app_domain: dev.example.com | |
# :admin_email: admin@example.com | |
# :development: | |
# <<: *defaults | |
# :test: | |
# <<: *defaults | |
# :production: | |
# <<: *defaults | |
# :app_domain: example.com | |
# | |
# For example will result in AppConfig.app_domain => "dev.example.com" | |
# when Rails.env == :development | |
# | |
class << self | |
def load(file='application.yml') | |
configuration_file = File.join Rails.root, 'config', file | |
File.open(configuration_file) do |configuration| | |
configuration = ERB.new(configuration.read).result | |
configuration = YAML.load(configuration)[Rails.env.to_sym] | |
configuration.each do |key, value| | |
cattr_accessor key | |
send "#{key}=", value | |
end | |
end if File.exists? configuration_file | |
end | |
end | |
end | |
AppConfig.load |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment