Skip to content

Instantly share code, notes, and snippets.

@dougal
Created November 30, 2009 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dougal/245623 to your computer and use it in GitHub Desktop.
Save dougal/245623 to your computer and use it in GitHub Desktop.
# Rails Config.
# Copyrighted(c) Douglas F Shearer 2009
# Licensed under The MIT License.
# http://douglasfshearer.com/blog/simple-rails-config
site:
url: http://example.com
authentication:
username: bob@example.com
password: myhackproofpassword
flickr:
api_key: d3c3576398a4876c920553b714bc177f
username: flickrusername
# Akismet.
wordpress:
api_key: 876c920553b7
# Rails Config Loader.
# Copyrighted(c) Douglas F Shearer 2009
# Licensed under The MIT License.
# http://douglasfshearer.com/blog/simple-rails-config
module Config
class ConfigStore
# Takes a hash as an argument.
# If this hash contains other hashes, these too will turned into
# ConfigStore objects.
def initialize(contents)
@contents = contents
@contents.each do |k, v|
if v.is_a?(Hash)
@contents[k] = self.class.new(v)
end
end
end
def method_missing(sym, *args)
@contents[sym.to_s] || super
end
end
def config
@@config ||= ConfigStore.new(YAML.load_file("#{RAILS_ROOT}/config/app.yml"))
end
end
include Config
>> config.authentication.username
=> "bob@example.com@"
>> config.authentication.password
=> "myhackproofpassword"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment