Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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).
Ruby
branch: 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
TODO
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.require! 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}

  # ...or if a warning note in the logs is enough:
  #   Yaml2env.require '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:

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

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

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

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

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

API

Being lazy and just dropping a lot of examples here.

Yaml2env.require

  # Case: If config file exists with proper keys
  Yaml2env.require 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
    => true
  Yaml2env.require 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
    => false + STDOUT: (already loaded warning)

  # Case: If config file don't exists, or it don't contain expected setting-key(s)
  Yaml2env.require 'config/hoptoad2.yml', {'HOPTOAD_API_KEY' => 'api_key'}
    => false + STDOUT: (invalid file or missing key warning)

Yaml2env.require!

See above: Same as Yaml2env.require but raises error instead of log warning.

Yaml2env.load

  # Case: If config file exists with proper keys
  Yaml2env.load 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
    => true

  # Case: If config file don't exists, or it don't contain expected setting-key(s)
  Yaml2env.load 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
    => STDOUT: (warning)

Yaml2env.load!

See above: Same as Yaml2env.require but raises error instead of log warning.

Yaml2env.assert_keys

  Yaml2env.assert_keys 'HOPTOAD_API_KEY'
    => true

  Yaml2env.assert_keys 'BAZOOKA'
    => false + STDOUT: (warning)

Yaml2env.assert_keys!

See above: Same as Yaml2env.assert_keys but raises error instead of log warning.

Yaml2env.assert_values

  Yaml2env.assert_values 'HOPTOAD_API_KEY' => /[a-z0-9]+/
    => true

  Yaml2env.assert_values 'HOPTOAD_API_KEY' => /[0-9]+/
    => false + STDOUT: (warning)

Yaml2env.assert_values!

See above: Same as Yaml2env.assert_values but raises error instead of log warning.

Yaml2env.log_root

  Yaml2env.log_root
    => STDOUT: :: Yaml2env.root = '/path/to/project/root'

Yaml2env.log_env

  Yaml2env.log_env
    => STDOUT: :: Yaml2env.env = 'development' (default)

Yaml2env.log_values

  Yaml2env.log_values
    => STDOUT: :: ENV = {'HOPTOAD_API_KEY' => 'api_key', 'RUBY_VERSION' => 'ruby-1.9.3-p0', ...}

  Yaml2env.log_values 'HOPTOAD_API_KEY'
    => STDOUT: :: ENV = {'HOPTOAD_API_KEY' => 'api_key'}

  Yaml2env.log_values 'BAZOOKA'
    => STDOUT: :: ENV = {}

  Yaml2env.log_values /RACK|MERCHII/
    => STDOUT: :: ENV = {'RACK_ENV' => 'api_key', 'MERCHII_ASSETS_DOMAIN' => 'assets.merchii.com'}

Yaml2env.detect_root!

  # For: Rack, Sinatra, Rails, or Yaml2env.default_env is set
  Yaml2env.detect_root!
    => Yaml2env.root = '/path/to/project/root'

  # For: Failed detection
  Yaml2env.detect_root!
    => DetectionFailedError

Yaml2env.detect_env!

  # For: Rack, Sinatra, Rails, or Yaml2env.default_env is set
  Yaml2env.detect_env!
    => Yaml2env.env = ENV['RACK_ENV']  # ...example for Rack

  # For: Failed detection
  Yaml2env.detect_env!
    => DetectionFailedError

  # For: Default environment set
  Yaml2env.default_env = 'development'
  Yaml2env.detect_env!
    => Yaml2env.env = 'development'

Yaml2env.loaded

  Yaml2env.loaded
    => STDOUT: '/path/to/project/root/config/hoptoad.yml' => {'HOPTOAD_API_KEY' => 'abc123'}

There are a few more, but these are the most useful ones.

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.