Skip to content

Instantly share code, notes, and snippets.

@jelder
Created September 7, 2012 18:02
Show Gist options
  • Save jelder/3668214 to your computer and use it in GitHub Desktop.
Save jelder/3668214 to your computer and use it in GitHub Desktop.
Alternative to Configatron compatible with the 12 Factor App philosophy.
# This is an attempt at constructing an application which complies with the
# ideas expressed at http://www.12factor.net/, specifically section 3, "Config"
# which states that configuration parameters which vary between environments
# should be stored in environment variables.
#
# `Configuration` is a thin wrapper around Ruby's ENV hash which also allows
# traditional custom config parameters to be set. Add an `attr_accessor` line
# and define the value in `config/environments/*.rb`.
#
# For example, `Configuration.api_token` called in the foobar environment will
# return the value `Configuration.api_token` set in
# `config/environments/foobar.rb`. Failing that, the value of $API_TOKEN is
# returned, or finally raise an exception if API_TOKEN is not set.
class Configuration
def self.method_missing(m, *args, &block)
key = m.to_s.upcase
if ENV.has_key? key
return ENV[key]
else
raise ConfigurationError.new(
"#{self.class.to_s}.#{m} is not set in config/environments/#{Rails.env}.rb, nor is the environment variable #{key} set."
)
end
end
class ConfigurationError < RuntimeError ; end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment