Skip to content
Stash environment-specific configs in YAML-files and load them into ENV according to best-practices pattern - and auto-detects on-initialization if something is missing (skipping the "scratching the head"-part).
Find file
Pull request Compare This branch is 1 commit ahead, 26 commits behind grimen:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
spec
.gitignore
.travis.yml
Gemfile
Guardfile
MIT-LICENSE
README.textile
Rakefile
yaml2env.gemspec

README.textile

YAML2ENV

Stash environment-specific configs in YAML-files and load them into ENV according to best-practices pattern – and auto-detects on-initialization if something is missing (skipping the “scratching the head”-part).

Motivation

For some rainy day…or next commit.

Frameworks

Yaml2env detects lean defaults for: Rack, Rails, and Sinatra. Though by setting Yaml2env.env and Yaml2env.root manually you are good with any Ruby-project.

Installation

Add to your Gemfile:

  gem 'yaml2env'

…and bundle install.

Usage

To give this some context; this is how we use Yaml2env to initialize Hoptoad:

  Yaml2env.load! 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}

  # ...or if a warning note in the logs is enough:
  #   Yaml2env.load 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}

  if defined?(HoptoadNotifier)
    HoptoadNotifier.configure do |config|
      config.api_key = ENV['HOPTOAD_API_KEY']
    end
  end

…and the corresponding YAML config file:

  development:
    api_key: NONE

  staging:
    api_key: 123abc

  production:
    api_key: abc123

  test:
    api_key: NONE

…which will yield:

  # Rails.env => 'development'
  ENV['HOPTOAD_API_KEY'] => 'NONE'

  # Rails.env => 'staging'
  ENV['HOPTOAD_API_KEY'] => '123abc'

  # Rails.env => 'production'
  ENV['HOPTOAD_API_KEY'] => 'abc123'

  # Rails.env => 'test'
  ENV['HOPTOAD_API_KEY'] => 'NONE'

  # Rails.env => 'other'
  => "Failed to load required config for environment 'other': /Users/grimen/development/example.com/config/hoptoad.yml"

Helpers

A few convenient helpers:

  Yaml2env.loaded? 'HOPTOAD_API_KEY'
  => true

  Yaml2env.loaded? 'BAZOOKA'
  => false

  Yaml2env.loaded? 'HOPTOAD_API_KEY', 'BAZOOKA'
  => false

Notes

This gem was developed for our own requirements at Merchii, so feel free to send pull-requests with enhancements of any kind (features, bug-fixes, documentation, tests, etc.) to make it better or useful for you as well.

License

Released under the MIT license.
Copyright © Jonas Grimfelt, Merchii

Something went wrong with that request. Please try again.