config_volumizer

Build Status Code Climate Coverage Status Dependency Status

Description

Ever wanted to move to ENV based 12 Factor config but are stuck with somewhat complex yaml (or simillar) files holding all your settings?

Been scratching your head how to convert your deeply nested config to ENV?

Config Volumizer comes to the rescue.

Examples

Convert this:

some:
  setting:
    - one
    - two
    - three
  with:
    another: setting

info a series of ENV variables like so:

some_setting = one,two,three
some_with_another = setting

... and then just give it some volume with the volumizer to turn it back to the original rich structure

mapping = { "some" => { "setting" => :value, "with" => { "another" => :value } } }
ConfigVolumizer.parse(ENV, mapping)

Features

Parsing

You can parse a flattened config via ConfigVolumizer.parse(ENV, mapping)

For example if your ENV was:

some_setting = one,two,three
some_with_another = setting

And you created a map like so:

mapping = {
  "some" => {
    "setting" => :value,
    "with" => {
      "another" => :value
    }
  }
}

This would yield a Hash like the following:

some:
  setting:
    - one
    - two
    - three
  with:
    another: setting

Fetching values (shorthand for parse)

You can fetch a specific key from the given source

For example:

env = {
  "some_setting" => "one,two,three",
  "some_with_another" => "setting",
}

mapping = {
  "setting" => [:value],
  "with" => {
    "another" => :value
  }
}

ConfigVolumizer.fetch(env, "some", mapping)

# returns:
{
 "setting" => [
   "one","two","three"
 ],
 "with" => {
   "another" => "setting",
 }
}

fetch works much like Hash#fetch, so you can pass an additional parameter for the default value, as well as use a block default value.

eg:

ConfigVolumizer.fetch(env, "another", mapping, "default_value")
# => "default_value"

ConfigVolumizer.fetch(env, "another", mapping) { |key| "default_#{key}" }
# => "default_another"

Generation

You can generate a flat list of configs (mostly as examples for your actual config) via: ConfigVolumizer.generate(data_hash)

For example, given a hash simillar to:

some:
  setting:
    - one
    - two
    - three
  with:
    another: setting

You would get back the data and mapping looking like this:

some:
  setting: :value
  with:
    another: :value
"some_setting": one,two,three
"some_with_another": setting

Deployment

In order to deploy a new version of the gem into the wild ...

You will need to configure your github api token for the changelog.

Generate a new token for changelogs here.

add:

export CHANGELOG_GITHUB_TOKEN=YOUR_CHANGELOG_API_TOKEN

somewhere in your shell init. (ie .zshrc or simillar)

vim lib/config_volumizer/version.rb
# set the new version
# commit the changed version file
# name your commit with the version number eg: "1.8.0"
rake release
# to push the gem to rubygems.org
rake changelog
# commit the changed changelog
# name your commit with the version again eg: "changelog for 1.8.0"
git push

Install

Add it to your gemfile and use it.

gem 'config_volumizer'

Copyright (c) 2015 PayrollHero Pte. Ltd.

See LICENSE.txt for details.