hconfig translates ENV vars into Config.*
attributes, with presence
validation and type casting. hconfig is intended to be used in
environments (Heroku, in my case) where much of the config is stored in ENV.
hconfig is a standalone gem version of the CastingConfigHelpers
module from
Pliny.
gem 'hconfig'
module Config
extend HConfig
mandatory :database_url, string
optional :versioning_default, string
override :port, 5000, int
end
ENV["DATABASE_URL"] = "postgres:///thing-db"
Config.database_url
# => "postgres:///thing-db"
If you're mixing this into a module named Config
, RbConfig will warn you
about doing so. You can supress the warning via:
Object.send(:remove_const, :Config) if Object.const_defined?(:Config)
Alternatively, you can just namespace Config
under your app/lib/etc.
An exception is raised when the ENV var is missing.
mandatory :database_url, string
The value is returned if set, otherwise nil
.
optional :versioning_default, string
The value is returned if set, otherwise the default value will be returned.
override :port, 5000, int
$ bundle install
$ rake