Project

configsl

0.0
The project is in a healthy, maintained state
A simple, modular, extensible DSL for configuration.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 3.1
 Project Readme

ConfigSL Gem Version Coverage Status Code Checks

ConfigSL is a simple Domain-Specific Language (DSL) module for configuration. It is designed to provide a declarative way to define configuration, with as few dependencies and additional cruft as possible. It is both modular and extensible, so you can use as little or as much as you need.

Installation

Add this line to your application's Gemfile:

gem 'configsl', '~> 1.1'

And then execute:

bundle install

Or install it yourself as:

gem install configsl

Usage

You can start defining your configurations using two methods:

  1. Extend the included ConfigSL::Config base class
  2. Include the ConfigSL modules you want to use in you class

Using the included base class

The ConfigSL::Config base class includes common functionality for working with configurations. Currently, the class provides the following features:

  • Collections: Supports arrays and hashes of other configurations
  • DSL: The primary DSL for defining configuration options
  • Format: A simple way to enforce option value formatting
  • FromEnvironment: Load configuration from environment variables
  • FromFile: Load configuration from a file
  • Merge: Merge configuration from multiple sources
  • ToHash: Recursively convert the configuration to a hash
  • Validation: Built-in validation for configuration options
require 'configsl'

class AppConfig < ConfigSL::Config
  register_file_format :json
  register_file_format :yaml

  option :name, type: String, default: 'My App'
  option :environment, type: Symbol, enum: %i[dev test prod], default: :dev,
                       env_variable: 'RACK_ENV'
  option :database, type: DatabaseConfig, required: true

  # Collect arrays or hashes into configuration objects. Automatically set a key
  # on the collected configurations based on their index (arrays) or key
  # (hashes).
  option :hosts, type: Hash, collection: { type: HostConfig, key: :hostname }

  # Use shorthand syntax if you don't need to set a key.
  option :plugins, type: Array, collection: PluginConfig
end

You can load your configuration from different sources using the following methods:

AppConfig.new(params) # Load from parameters
AppConfig.from_file # Load from a file
AppConfig.from_environment # Load from environment variables
AppConfig.load # Merge from multiple sources

Including modules

If you'd like to pick and choose the features you want to use, you can include modules individually. Most modules can be included in any order, but the DSL module must be included before any others.

Additionally, you will need to implement initialize -- or some other method -- that sets the configuration values by calling set_value for each option.

require 'configsl'

class ApplicationConfig
  include ConfigSL::DSL
  include ConfigSL::Format
  include ConfigSL::FromEnvironment

  option :name, type: String, default: 'My App'
  option :environment, type: Symbol, env_variable: 'RACK_ENV'
  option :database, type: DatabaseConfig

  def initialize(params = {})
    params.each do |name, value|
      set_value(name, value)
    end
  end
end

A note about inheritance

When working with multiple configuration classes, you'll likely want to use a base class. This could be the ConfigSL::Config class, or a custom class that includes the modules you need.

While the modules you include are inherited by subclasses, any options you define via DSL are not. This is because these values are stored using class instance variables. As a result, if you have options that are shared between classes, they will need to be implemented in both.

It's important to note that this is not limited to your defined configuration options, but also methods such as register_file_format and config_file_path.