Gaston is a store configuration that can be used in any Ruby project, it was released a bit more than year ago. We use it on most of our Ruby projects but Gaston didn't have the honours of a blog post yet.
Installation
Ruby 1.9.2 is required.
Install it with rubygems:
gem install gaston
With bundler, add it to your Gemfile
:
gem "gaston"
Environment
Let's take a look at this yaml file containing some configuration values.
:gaston:
:api:
:state: "awesome"
:development:
:api:
:token: "api_token"
:test:
:api:
:token: <%= ENV["API_TOKEN"] %>
Gaston allows to specify different values depending on the environment. The
gaston
environment represents the default values that all environments
inherit, unless a given value is overwritten in a specific environment.
In development
environment, calling Gaston.api
will yield the following
hash:
{
"state" => "awesome",
"token" => "api_token"
}
In test
environment (with the Environment variable set to
"269596961710045034"), calling Gaston.api
will yield the following hash:
{
"state"=>"awesome",
"token" => "269596961710045034"
}
This example also shows that gaston accepts Ruby code as a value through ERB templating.
Gaston also accepts json files:
{
"gaston":{
"hostname":"dev.af83.com"
}
}
The same rules apply here.
A small setup is required to use Gaston. You can define an environment with the
env
method and specify config files with the files
method. Default env
is
:development
.
require 'gaston'
Gaston.configure do |gaston|
gaston.env = ENV['ENV']
gaston.files = Dir['*.yml', '*.json']
end
Gaston.api
# => {:state=>"awesome", :token=>"api_token"}
Gaston.hostname
# "dev.af83.com"
One last thing
At its core, Gaston inherits from the Ruby core class Hash
. A Gaston store
give access to all the methods
defined on the Hash class. So you can use the classic keys
, fetch
methods.
In order to do that, Gaston does not allow key names that are also a method
defined on Hash. In this case, Gaston does not add this part of the
configuration and logs a warning.