Fig

DRY up those magic numbers and hard coded strings into something more managable. Fig is the smart way to manage stuff that really belongs in a config file instead of scattered throughout your code.

Why

I know that Ruby is a dynamic language and that hard coded values aren’t as big a deal, but… it’s still good practice to have a centralized place to hold certain settings.

I used to create a globally available Hash to store this stuff, but have always wanted something more powerful and elegant.

What

Fig is a simple to use configuration management tool for Ruby applications and libraries.

The primary features are:

  • Multiple configuration files

  • Dynamic updating and re-loading of configuration files

  • Simple dot-notation access to configuration settings

  • YAML access to configuration settings

  • Safe options to request settings that may not exist

  • Interpolation to help DRY up configuration files through reuse of settings

  • Thread safe

How

The simplest way to get started is to watch the 10 minute tutorial.

Installation

Fig is availabe as both a Gem and as a Rails Plugin.

To install as a Gem:

First, be sure that Github has been added as a gem source. (This only needs to be done once.)

gem sources -a http://gems.github.com

Second, install the Gem.

sudo gem install hopsoft-fig

To install as a Rails Plugin:

script/plugin install git://github.com/hopsoft/fig.git

Usage

Create a YAML file that will serve as one of the configuration files you plan to use. In a Rails application, I usually create the file config/app.yml, but you can name the file anything you like and can save it to any location within your appliation or library.

Require Fig either explicitly or implicitly. In a Rails application, I generally do this in environment.rb.

# implicit
require 'hopsoft/fig'
# explicit
gem 'hopsoft-fig'

Instantiate a Fig object that is globally available to your application. In a Rails application, I generally do this in environment.rb.

APP_CONFIG = Fig.new(RAILS_ROOT + '/config/app.yml')

Start using your settings.

puts APP_CONFIG.settings.message
puts APP_CONFIG.yaml['message']
puts APP_CONFIG.get_setting('message')
# returns nil instead of an error when the setting doesn't exist
puts APP_CONFIG.get_setting('some.nested.setting.that.may.not.exist')

Reuse settings in your YAML file (This is a great way to apply the DRY principle to your configuration settings):

name: Nathan Hopkins
message: {fig:name} says hello.

puts APP_CONFIG.get_setting('message')
# outputs -> Nathan Hopkins says hello.

Update the YAML file and load the changes without restarting your application.

APP_CONFIG.load

Copyright © 2008 Hopsoft LLC, released under the MIT license