Skip to content

Instantly share code, notes, and snippets.

@maccman
Created August 28, 2011 12:15
Show Gist options
  • Save maccman/1176596 to your computer and use it in GitHub Desktop.
Save maccman/1176596 to your computer and use it in GitHub Desktop.
Rails 3 Config
# 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
@ismasan
Copy link

ismasan commented Aug 28, 2011

Why not use an OpenStruct?

@maccman
Copy link
Author

maccman commented Aug 28, 2011

Because I'm an idiot :) - thanks!

@maccman
Copy link
Author

maccman commented Aug 28, 2011

Although there is one good reason - I want hash values to return another config object, so I can do:

Rails.config.github.another.anotherone

@bkeepers
Copy link

@maccman
Copy link
Author

maccman commented Aug 28, 2011

@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