Globalize Your Configuration

Quite a lot of applications face the problem of storing configuration data, where configuration differs for development, test and production environments.

Yaml files is a common way of organising application settings. They help make our configuration clean, as well as allow loading it somewhere to constant or global variable and then re-use in application.

But it would be really cool to have some abstract interface for all our configuration files in an application and
global gem resolves this issue by providing object oriented interface for our config files.

Installation

Extremely simple:

or you can also insert the following line into Gemfile:

Configuration

Before using the gem, we need to configure directory that is used as a source to load yaml files and our application environment:

For Rails, put initialisation into config/initializers/global.rb:

Usage

General

Let’s say our application has web and api parts divided by different hostnames. Also, each environment has its own configuration. We place all this stuff to config/global/hosts.yml file with the following structure:

Now, for the development environment we have the following data:

Namespacing

If there are different settings groups, we can split the files by directories in order to make configuration more organised. For example, let’s say web should have basic auth.
In this case we can have config/global/web/basic_auth.yml with:

Then in the development environment we will have:

Default section

In our previous examples, development and test sections were quite similar. To DRY our configs, use default section:

Data from the default section is used until it’s overridden in a specific environment.

ERB support

Global supports erb injection to make configuration dynamic. For this, place the following code into ‘global/file_name.yml’

As a result, here’s what we have in the development environment:

References

For more details, check out the source code and documentation on github.