0.0
No commit activity in last 3 years
No release in over 3 years
Verifies the configurations against the specifications in a YAML file
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
~> 10.3
~> 3.0
 Project Readme

SettingsSpec

Build Status

There are many tools to load configurations in Rails, such as rais_config, settingslogic. Or you can just load your configurations in a YAML file directly, YAML.load_file("config.yml").

Normally, there will be a file named config_sample.yml, where you write all configurations. When you deploy the application, you make an copy of the sample file, change some values and launch the application to see if it works or not.

And later, you add some new entries into the sample file as the application grows. You must remember to add them to the configuration file on production when you update the servers. Otherwise, the application may refuse to work, complaining about missing configurations.

This gem is built to address the problem. It converts the sample file into a specification file. Instead of giving some sample values, you write rules to validate the configuration file, so that you know the configuration file is all right before an invalid configuration file breaks the application.

It is tested on Ruby 1.9.3, 2.1.2. It should also work on Ruby 1.8.7, but you have to use lambda rather than ->.

Installation

Add this line to your application's Gemfile:

gem 'settings_spec'

And then execute:

$ bundle

Or install it yourself as:

$ gem install settings_spec

Usage

It supports following rules in the specification file:

  • gt n: the number should greater than n
  • lt n: the number should less than n
  • match <regexp>: match a string against a regexp
  • one_in <array_or_range>: the value should be included in the given array_or_range
  • all_in <array_or_range>: the value should be an array, which is a subset of the given array_or_range
  • is_a <class>: the value should be a type of class
  • blank: the value can be blank, meaning this entry is optional
  • call <proc>: calls a proc to validate the value
  • all above rules can be combined with and, or, and grouped with ()

A specification file could look like this:

defaults: &defaults
  api:
    url: match /^https/
    username: one_in %w{user1 user2}
    password: blank or match /\A.{8,}\z/
    retries: one_in 1..9
    enabled_env: all_in %w{staging production}
    reconnect: one_in [true false]
    pool: gt 3
  mod3: call ->(n){ n % 3 == 0}

development:
  <<: *defaults

test:
  <<: *defaults

It can validate any settings system which responds to :<key> or :[].

Suppose the specification file is loaded this way:

spec_file = Rails.root.join('config', 'config_spec.yml')
specs = SettingsSpec.load(spec_file, Rails.env)

Validate settingslogic

class Settings < Settingslogic
  source Rails.root.join('config', 'config.yml')
  namespace 'development'
end
specs.verify!(Settings)

Validate rails_config

RailsConfig.load_and_set_settings Rails.root.join('config', 'config.yml')
specs.verify!(Settings)

Validate app_config

AppConfig.setup!({yaml: Rails.root.join('config', 'config.yml'), env: Rails.env})
specs.verify!(AppConfig)

Validate YAML

::AppConfig = YAML.load_file(Rails.root.join('config', 'config.yml'))['development']
specs.verify!(AppConfig)

Contributing

  1. Fork it https://github.com/YanhaoYang/settings_spec/fork
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request