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:
gem install global
or you can also insert the following line into Gemfile:
gem 'global'
Configuration
Before using the gem, we need to configure directory that is used as a source to load yaml files and our application environment:
Global.environment = "ENV_HERE"
Global.config_directory = "PATH_TO_DIRECTORY"
For Rails, put initialisation into
config/initializers/global.rb
:
Global.environment = Rails.env.to_s
Global.config_directory = Rails.root.join('config/global').to_s
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:
test:
web: localhost
api: api.localhost
development:
web: localhost
api: api.localhost
production:
web: myhost.com
api: api.myhost.com
Now, for the development environment we have the following data:
> Global.hosts
=> { "api" => "api.localhost", "web" => "localhost" }
> Global.hosts.api
=> "api.localhost"
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:
test:
username: user
password: secret
development:
username: user
password: secret
production:
username: production_user
password: supersecret
Then in the development environment we will have:
> Global.web.basic_auth
=> { "username" => "development_user", "password" => "secret" }
> Global.web.basic_auth.username
=> "development_user"
Default section
In our previous examples, development and test sections were quite similar. To DRY our configs, use
default
section:
default:
web: localhost
api: api.localhost
production:
web: myhost.com
api: api.myhost.com
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’
test:
key: <%=1+1%>
development:
key: <%=2+2%>
production:
key: <%=3+3%>
As a result, here’s what we have in the development environment:
> Global.file_name.key
=> 4
References
For more details, check out the source code and documentation on
github.