-
-
Save shreyas-satish/1176701 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 | |
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 self.new(value) | |
return super if value.is_a?(Hash) | |
value | |
end | |
attr_reader :attributes | |
def initialize(attributes) | |
@attributes = attributes | |
end | |
def method_missing(method_symbol, *arguments) #:nodoc: | |
method_name = method_symbol.to_s | |
if method_name =~ /(=|\?)$/ | |
case $1 | |
when "=" | |
attributes[$`] = arguments.first | |
when "?" | |
attributes[$`] | |
end | |
else | |
if attributes.include?(method_name) | |
return self.class.new(attributes[method_name]) | |
end | |
super | |
end | |
end | |
end | |
def self.config | |
@config ||= Config.load | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment