Skip to content

Instantly share code, notes, and snippets.

@shreyas-satish
Forked from maccman/config.rb
Created August 28, 2011 14:04
Show Gist options
  • Save shreyas-satish/1176701 to your computer and use it in GitHub Desktop.
Save shreyas-satish/1176701 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
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