https://github.com/grimen/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).
https://github.com/grimen/yaml2env
Last synced: 3 months ago
JSON representation
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).
- Host: GitHub
- URL: https://github.com/grimen/yaml2env
- Owner: grimen
- License: mit
- Created: 2011-07-24T00:57:55.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2012-07-16T21:04:01.000Z (almost 13 years ago)
- Last Synced: 2025-02-01T13:04:15.249Z (4 months ago)
- Language: Ruby
- Homepage: https://rubygems.org/gems/yaml2env
- Size: 154 KB
- Stars: 1
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.textile
- License: MIT-LICENSE
Awesome Lists containing this project
README
h1. YAML2ENV "!https://secure.travis-ci.org/merchii/yaml2env.png!":http://travis-ci.org/merchii/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)._
h2. Motivation
For some rainy day...or next commit.
h2. Frameworks
@Yaml2env@ detects lean defaults for: *"Rack":https://github.com/rack/rack*, *"Rails":https://github.com/rails/rails*, and *"Sinatra":https://github.com/sinatra/sinatra*. Though by setting @Yaml2env.env@ and @Yaml2env.root@ manually you are good with any Ruby-project.
h2. Installation
Add to your @Gemfile@:
gem 'yaml2env'...and @bundle install@.
h2. Usage
To give this some context; this is how we use @Yaml2env@ to initialize "Hoptoad":http://hoptoadapp.com:
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: NONEstaging:
api_key: 123abcproduction:
api_key: abc123test:
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"h2. 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'
=> trueYaml2env.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]+/
=> trueYaml2env.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.
h2. Notes
This gem was developed for our own requirements at *"Merchii":http://github.com/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.
h2. License
Released under the MIT license.
Copyright (c) "Jonas Grimfelt":http://github.com/grimen, "Merchii":http://github.com/merchii