Created
January 19, 2010 03:54
-
-
Save stephencelis/280650 to your computer and use it in GitHub Desktop.
App update
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
# Define application-level configuration using a simple DSL. | |
# | |
# class App < Configuration | |
# config.key = "value" | |
# config.lazy = lambda { |load| load } | |
# end | |
# | |
# App.key # => "value" | |
# App.key? # => true | |
# App.lazy("load") # => "load" | |
# App.lazy?(nil) # => false | |
# | |
# Override by Rails environment. | |
# | |
# # config/app/development.rb | |
# config.key = "devalue" | |
# | |
# App.key # => "devalue" | |
class Configuration | |
autoload :OpenStruct, "ostruct" | |
class << self | |
def [](key) | |
config.send key | |
end | |
def inspect | |
config.inspect.sub config.class.name, name | |
end | |
private | |
def config | |
@config ||= OpenStruct.new | |
end | |
def method_missing(method, *args) | |
super if (key = method.to_s).end_with?("=") | |
boolean = key.chomp!("?") | |
value = self[key] | |
value = value.call(*args) if value.respond_to?(:call) | |
boolean ? !!value : value | |
end | |
def inherited(subclass) | |
return unless defined? Rails | |
config = "#{Rails.root}/config/#{name.underscore}/#{Rails.env}.rb" | |
if File.exist?(config) | |
eval File.read(config), binding, __FILE__, __LINE__ | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment