Repository is archived
No commit activity in last 3 years
No release in over 3 years
Rack::Config::Flexible is an alternative to Rack::Config, offering much greater flexibility. Configuration options are stored as key-value pairs in _sections_, partitioned by _environments_. For example: + environment + section key -> value pairs A simple DSL is provided and can be used either within a passed configuration block (to ::new), or to the #configuration method. Facilities are also provided to load whole environments, and sections from either a single YAML file structured like, or from a directory tree. See the README file or RDoc documentation for more info.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

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.