New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rack-config-flexible

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rack-config-flexible

  • 0.1.4
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

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.

FAQs

Package last updated on 19 Nov 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc