configerator
Simple module for implementing environment based configuration following the The Twelve-factor App pattern.
This was adapted from the configuration implementation in Pliny.
Installation
Add this line to your application's Gemfile:
gem 'configerator'And then execute:
$ bundle
Or install it yourself as:
$ gem install configerator
Usage
Methods
-
required :key- Require a key, raise a
KeyErrorif key is not supplied on application start up.
- Require a key, raise a
-
required :key, error_on_load: false- Require a key, raise a
RuntimeErrorif key is not supplied whenkeyis requested.
- Require a key, raise a
-
optional :key- Create
key, set tonilif not present.
- Create
-
override :key, :value- Create
key, set tovalueif 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?Rails
You can generate a config file, thusly:
rails generate configerator:configThis 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'Pure Ruby
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
endAuto-detecting Dotenv
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-railsmethod to load your.envfiles. This may be of some surprise when it loads both your.envand.env.testconfigurations.
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.
Disabling
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'Development
Testing
# 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