rack-config-flexible

A flexible configuration middleware for Rack that provides a simple DSL, as well as the ability to easily load configuration from yaml files.

Licensing

This software is licensed under the Simplified BSD License as described in the LICENSE file.

Installation

gem install rack-config-flexible

Usage

Just add something like this to your config.ru:

require 'rack/config/flexible'

use Rack::Config::Flexible do
  environment :production
    section :data
      set :key => 'value'

  environment :development
    section :data
      set :key => 'dev_value'

  # Set the current environment
  environment :production
end

Of course, individual environments, sections, and even the entire configuration can be loaded from yaml files.

Accessing the Configuration Data

The configuration can be accessed by downstream middleware via the Rack environment. In the Usage example, you could access key's value as follows:

env['rack.config']['data.key']

and you can even modify values as follows:

env['rack.config']['data.key'] = 'new_value'

if, and only if, the given key exists. The format for the hash key is section.key.

Loading an Environment/Section from Yaml

require 'rack/config/flexible'

use Rack::Config::Flexible do
  environment :production
    section :data, 'cfg/production/data.yaml'

  environment :development, 'cfg/development.yaml'

  # Set the current environment
  environment :production
end

Any calls to set after environment or section will override data loaded from the yaml file if the same key is specified. Otherwise, they'll just add the values to the hash per usual.

Loading the Entire Configuration from Yaml

You can load the entire configuration from a single file, or a directory tree.

This example loads from a single file:

require 'rack/config/flexible'

use Rack::Config::Flexible :from_file => 'settings.yaml' do
  # Set the current environment to production
  environment :production
end

The settings.yaml file should be laid out like:

environment:
  section:
    key: value

So, the equivalent of the initial example would be:

production:
  data:
    key: value

development:
  data:
    key: dev_value

This example loads from a directory tree:

require 'rack/config/flexible'

use Rack::Config::Flexible :from_file => 'settings' do
  # Set the current environment to production
  environment :production
end

The directory tree is expected to be laid out like:

settings/environment/section.yaml

Where each directory under settings is an environment, containg a separate yaml file for each section. The yaml file itself will only hold key-value pairs for that particular section.

See the inline documentation for more details.