Installation

gem install global

gem 'global'

Configuration

Global.environment = "ENV_HERE" Global.config_directory = "PATH_TO_DIRECTORY"

config/initializers/global.rb

Global.environment = Rails.env.to_s Global.config_directory = Rails.root.join('config/global').to_s

Usage

General

config/global/hosts.yml

test: web: localhost api: api.localhost development: web: localhost api: api.localhost production: web: myhost.com api: api.myhost.com

> Global.hosts => { "api" => "api.localhost", "web" => "localhost" } > Global.hosts.api => "api.localhost"

Namespacing

config/global/web/basic_auth.yml

test: username: user password: secret development: username: user password: secret production: username: production_user password: supersecret

> Global.web.basic_auth => { "username" => "development_user", "password" => "secret" } > Global.web.basic_auth.username => "development_user"

Default section

default

default: web: localhost api: api.localhost production: web: myhost.com api: api.myhost.com

ERB support

test: key: <%=1+1%> development: key: <%=2+2%> production: key: <%=3+3%>

> Global.file_name.key => 4

References

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.Extremely simple:or you can also insert the following line into Gemfile: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 intoLet’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 tofile with the following structure:Now, for the development environment we have the following data: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 havewith:Then in the development environment we will have:In our previous examples, development and test sections were quite similar. To DRY our configs, usesection:Data from the default section is used until it’s overridden in a specific environment.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:For more details, check out the source code and documentation on github