SugarfreeConfig

Configuration handling the easy way.

SugarfreeConfig allows easy access to per Rails environment config values stored in a YAML file.

SugarfreeConfig only works with Rails! since SugarfreecConfig 1.1.0 works with rails 3 and ruby 1.9.1

Installation

Just install the gem from Gemcutter

$ gem install sugarfree-config --source http://gemcutter.org

Notes on rails 3

In rails 2.x gems were required after Rails.root was initialized. Default rails 3.0 application generator uses Bundler, and auto-requires all gems before application initialization. This could be solved forcing the bundler not to require the gem. In your Gemfile:

gem 'problematic-gem', :require => false

and adding an initializer with the require line

require 'problematic-gem'

Usage

Config must be stored in “#RAILS_ROOT/config/config.yml” and must have at least a section for each RAILS_ENV

Configuration can be accessed like this

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

which will expect a config file like (given development env)

development:
  a:
    b:
      c: 'value'

otherwise a ConfigKeyException will be raised

Configuration is accessed through a ConfigIterator that (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'

Further customization

By default Sugarfree-config caches in memory the YAML file. If you want the gem to reload this file each time a value is accessed (like in development mode) you must force the initialization of the SugarfreeConfig module.

This could be a possible Rails initializer:

SugarfreeConfig.init(Rails.env.development?) # True to force reload each time

class Object
  # For people who prefer app_config.a.b.c instead of SugarfreeConfig.a.b.c
  def app_config
    SugarfreeConfig
  end
end

Contact

Please mail bugs, suggestions and patches to [email protected]