Configuration handling the easy way
Ruby
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
specs
.gitignore
Gemfile
Gemfile.lock
LICENSE
README.rdoc
Rakefile
init.rb
sugarfree-config.gemspec

README.rdoc

sugarfree-config

Configuration handling the easy way.

sugarfree-config allows easy access to per environment config values stored in a YAML file in Rails and non Rails applications.

Installation

Just install the gem manually from rubygems.org or add it to your bundle.

$ gem install sugarfree-config

Usage

Configuration can be accessed using method calls:

SugarfreeConfig.a.b.c #=> 'value'

which will expect a config.yml file like the one below (given development env) stored in the config folder (in Rails apps) or the root folder (in non Rails apps):

development:
  a:
    b:
      c: 'value'

If asking for akey that does not exists ConfigKeyException will be raised.

A convinience to_hash method is also provided:

SugarfreeConfig.a.to_hash #=> { 'b' => { 'c' => 'value' } }

Configuration

sugarfree-config can be used with or without Rails. Default configurations are provided for each case. Default configurations can be overriden the explicit use of the SugarfreeConfig::init method.

To set the configuration you need to initialize the SugarfreeConfig class:

SugarfreeConfig.init :env => 'development', :file => 'my_config.yml', :reload => true

# Default options in Rails apps
SugarfreeConfig.init({
  :env => Rails.env,
  :file => Rails.root.join('config', 'config.yml'),
  :reload => Rails.env.development?
})

# Default options in non Rails apps
SugarfreeConfig.init({
  :env => 'development',
  :file => File.expand_path('config.yml'),
  :reload => false
})

Gotchas

Configuration is accessed through a ConfigIterator class which (as its name says) is an iterator and cannot be reused. However there is a workaround to allow the repeated use of scoped iterators.

# This doesn't work
var a_config = SugarfreeConfig.a
a_config.b.c #=> 'value'
a_config.b.c #=> Ups!!! tried to grab config.a.b.c.b.c

# Workaround
var a_config = SugarfreeConfig.a
a_config.dup.b.c #=> 'value'
a_config.dup.b.c #=> 'value'

Compatibility

SugarfreeConfig 2.0.0 has been developed and tested against Ruby 1.9.2 and Rails 3.x. Older versions of Ruby and Rails may also work. Just give it a try ;)

Contact

Please use GitHub (github.com/davidbarral/sugarfree-config) to report bugs, make suggestions or send patches.