dns.yml — loaderio-web 2013-07-26 13-25-18

Globalize Your Configuration

| 5 Comments

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.

Share
* Railsware is a premium software development consulting company, focused on delivering great web and mobile applications. Learn more about us.
  • http://enlivan.com/ Damien Roche

    Fantastic gem, along the lines of figaro, which I’m currently using. What I find confusing, however, is the naming. When I use Global.web.basic_auth I do not expect a namespaced web.basic_auth in return simply because of the constant Global. If a developer was introduced to a project where this gem was being used, and didn’t add it themselves, would they assume the same thing?

    • Olexander Paladiy

      Damien, could you explain in details what exactly confuses you?

  • Olexander Paladiy

    The main difference is access to multiple yaml files through ruby interface. So that you can split your yaml file and make configuration much maintainable.

  • Pingback: Rails configuration in the proper way | Railsware Blog()

  • Vladimir Melnick (rubydev.ru)

    I don’t understand what this gem does except multiple calls of method_missing. It adds only performance issues to your code and nothing else.

Want to get more of Railsware blog?

RSS FEED

We're always ready to help!

CONTACT US