Created
August 28, 2011 12:15
-
-
Save maccman/1176596 to your computer and use it in GitHub Desktop.
Rails 3 Config
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
# Rails 3 Config | |
# | |
# In: config/application.yml | |
# | |
# development: | |
# github: | |
# key: test | |
# secret: verysecret-dev | |
# production: | |
# github: | |
# key: test | |
# secret: verysecret-dev | |
# | |
# Usage: | |
# Rails.config.github.key | |
# Rails.config.github.secret? | |
module Rails | |
class Config < ActiveSupport::OrderedOptions | |
def self.load(path = nil) | |
path ||= Rails.root.join('config', 'application.yml') | |
config = YAML.load(path.read) | |
new(config[Rails.env.to_s] || config) | |
end | |
def initialize(options = {}) | |
super() | |
options.each {|k, v| self[k] = v } | |
end | |
def [](key) | |
value = super(key) | |
value = self.class.new(value) if value.is_a?(Hash) | |
value | |
end | |
end | |
def self.config | |
@config ||= Config.load | |
end | |
end |
Because I'm an idiot :) - thanks!
Although there is one good reason - I want hash values to return another config object, so I can do:
Rails.config.github.another.anotherone
See https://github.com/rails/rails/blob/master/activesupport/lib/active_support/ordered_options.rb, which is what the Rails config uses.
@bkeepers - awesome, thanks - I've updated it to use that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not use an OpenStruct?