Simple module for implementing environment based configuration following the The Twelve-factor App pattern.
This was adapted from the configuration implementation in Pliny.
Add this line to your application's Gemfile:
gem 'configerator'
And then execute:
$ bundle
Or install it yourself as:
$ gem install configerator
required :key
- Require a key, raise a
KeyError
if key is not supplied on application start up.
- Require a key, raise a
required :key, error_on_load: false
- Require a key, raise a
RuntimeError
if key is not supplied whenkey
is requested.
- Require a key, raise a
optional :key
- Create
key
, set tonil
if not present.
- Create
override :key, :value
- Create
key
, set tovalue
if not present.
- Create
namespace :name { optional :key }
- Namespaces a collection of keys — e.g.
name_key
- Creates a validator for all defined keys in the block — e.g.
name?
- Skip prefixing namespace for variables and methods with
prefix: false
- Namespaces a collection of keys — e.g.
# namespace example
namespace :aws do
required :token, string
required :secret, string
optional :region, string
end
# where
aws?
#=> true # if aws_token? && aws_secret? && aws_region?
# namespace without prefix
namespace :etc, prefix: false do
required :foo, string
required :bar, string
end
# where
etc?
#=> true # if foo? && bar?
You can generate a config file, thusly:
rails generate configerator:config
This will generate a configuration into config/config.rb
with tips in comments.
Over time your config will be customized to your app, for example:
# file: config/config.rb
require 'configerator'
module Config
extend Configerator
required :something, string
optional :anotherthing, string
override :port, 3000, int
end
# file: config/application.rb
require_relative 'boot'
require 'rails/all'
# after to let rails framework to load first
require_relative 'config'
As a module
require 'configerator'
module Config
extend Configerator
required :something, string
optional :anotherthing, string
override :port, 3000, int
end
puts "#{Config.something}, and maybe: '#{Config.anotherthing}', all with #{Config.port}"
Included
require 'configerator'
class Foo
include Configerator
def initialize
required :something, string
optional :anotherthing, string
override :port, 3000, int
end
def run
puts "Doing %s with %s on %s" % [ something, anotherthing, port ]
end
end
Configerator will autodetect Dotenv or
dotenv-rails
and load it so that its environment variables are available
as soon as the Configerator is ready.
NOTE: For Rails projects, Configerator uses the
dotenv-rails
method to load your.env
files. This may be of some surprise when it loads both your.env
and.env.test
configurations.
When dotenv-rails
initializes, it loads environment files in this order:
.env.local
.env.$RAILS_ENV
.env
We recommend that you use .env.development
instead of .env
for your
development configurations and .env.test
for your test configurations.
One nice side effect is you will no longer have to Dotenv.load(".env.test")
in your test_helper.rb
/spec_helper.rb
.
You would then use .env
for shared configurations across all environments.
Of course if this doesn't work for your needs you can bypass autodetecting
and loading of Dotenv
. You can do this by requiring the library directly:
require 'configerator/configerator'
# w/ docker
$ docker-compose --rm test
# w/o docker
$ make
# w/o make and docker
$ bundle install
$ bundle exec ruby -Ilib:test ./test/*_test.rb