yaml2env 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Guardfile +10 -0
- data/README.textile +80 -0
- data/Rakefile +2 -0
- data/lib/yaml2env/version.rb +3 -0
- data/lib/yaml2env.rb +126 -0
- data/spec/fixtures/example.yml +19 -0
- data/spec/spec_helper.rb +49 -0
- data/spec/yaml2env_spec.rb +351 -0
- data/yaml2env.gemspec +28 -0
- metadata +155 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/README.textile
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
h1. YAML2ENV
|
2
|
+
|
3
|
+
_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)._
|
4
|
+
|
5
|
+
h2. Motivation
|
6
|
+
|
7
|
+
For some rainy day...or next commit.
|
8
|
+
|
9
|
+
h2. Frameworks
|
10
|
+
|
11
|
+
@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.
|
12
|
+
|
13
|
+
h2. Installation
|
14
|
+
|
15
|
+
Add to your @Gemfile@:
|
16
|
+
|
17
|
+
<pre>
|
18
|
+
gem 'yaml2env'
|
19
|
+
</pre>
|
20
|
+
|
21
|
+
...and @bundle install@.
|
22
|
+
|
23
|
+
h2. Usage
|
24
|
+
|
25
|
+
To give this some context; this is how we use @Yaml2Env@ to initialize "Hoptoad":http://hoptoadapp.com:
|
26
|
+
|
27
|
+
<pre>
|
28
|
+
Yaml2Env.load 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
29
|
+
|
30
|
+
if defined?(HoptoadNotifier)
|
31
|
+
HoptoadNotifier.configure do |config|
|
32
|
+
config.api_key = ENV['HOPTOAD_API_KEY']
|
33
|
+
end
|
34
|
+
end
|
35
|
+
</pre>
|
36
|
+
|
37
|
+
...and the corresponding YAML config file:
|
38
|
+
|
39
|
+
<pre>
|
40
|
+
development:
|
41
|
+
api_key: NONE
|
42
|
+
|
43
|
+
staging:
|
44
|
+
api_key: 123abc
|
45
|
+
|
46
|
+
production:
|
47
|
+
api_key: abc123
|
48
|
+
|
49
|
+
test:
|
50
|
+
api_key: NONE
|
51
|
+
</pre>
|
52
|
+
|
53
|
+
...which will yield:
|
54
|
+
|
55
|
+
<pre>
|
56
|
+
Rails.env = 'development'
|
57
|
+
ENV['HOPTOAD_API_KEY'] => 'NONE'
|
58
|
+
|
59
|
+
Rails.env = 'staging'
|
60
|
+
ENV['HOPTOAD_API_KEY'] => '123abc'
|
61
|
+
|
62
|
+
Rails.env = 'production'
|
63
|
+
ENV['HOPTOAD_API_KEY'] => 'abc123'
|
64
|
+
|
65
|
+
Rails.env = 'test'
|
66
|
+
ENV['HOPTOAD_API_KEY'] => 'NONE'
|
67
|
+
|
68
|
+
Rails.env = 'other'
|
69
|
+
=> "Failed to load required config for environment 'other': /Users/grimen/development/example.com/config/hoptoad.yml"
|
70
|
+
</pre>
|
71
|
+
|
72
|
+
h2. Notes
|
73
|
+
|
74
|
+
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.
|
75
|
+
|
76
|
+
h2. License
|
77
|
+
|
78
|
+
Released under the MIT license.
|
79
|
+
Copyright (c) "Merchii":http://github.com/merchii, "Jonas Grimfelt":http://github.com/grimen
|
80
|
+
|
data/Rakefile
ADDED
data/lib/yaml2env.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module Yaml2env
|
5
|
+
|
6
|
+
autoload :VERSION, 'yaml2env/version'
|
7
|
+
|
8
|
+
class Error < ::StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
class DetectionFailedError < Error
|
12
|
+
end
|
13
|
+
|
14
|
+
class ConfigLoadingError < Error
|
15
|
+
end
|
16
|
+
|
17
|
+
class MissingConfigKeyError < Error
|
18
|
+
end
|
19
|
+
|
20
|
+
# Hash tracking all of the loaded ENV-values via Yaml2env.load.
|
21
|
+
LOADED_ENV = {}
|
22
|
+
|
23
|
+
# Config root.
|
24
|
+
# Default: (auto-detect if possible)
|
25
|
+
@@root = nil
|
26
|
+
|
27
|
+
# Config environment.
|
28
|
+
# Default: (auto-detect if possible)
|
29
|
+
@@env = nil
|
30
|
+
|
31
|
+
# Logger to use for logging.
|
32
|
+
# Default: +::Logger.new(::STDOUT)+
|
33
|
+
@@logger = ::Logger.new(::STDOUT)
|
34
|
+
|
35
|
+
class << self
|
36
|
+
|
37
|
+
[:root, :env, :logger].each do |name|
|
38
|
+
define_method name do
|
39
|
+
class_variable_get "@@#{name}"
|
40
|
+
end
|
41
|
+
define_method "#{name}=" do |value|
|
42
|
+
class_variable_set "@@#{name}", value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
alias :environment :env
|
47
|
+
|
48
|
+
def defaults!
|
49
|
+
@@root ||= nil
|
50
|
+
@@env ||= nil
|
51
|
+
@@logger ||= ::Logger.new(::STDOUT)
|
52
|
+
end
|
53
|
+
|
54
|
+
def configure
|
55
|
+
yield self
|
56
|
+
end
|
57
|
+
|
58
|
+
def load(config_path, required_keys = {}, optional_keys = {})
|
59
|
+
self.detect_root!
|
60
|
+
self.detect_env!
|
61
|
+
|
62
|
+
config ||= {}
|
63
|
+
|
64
|
+
begin
|
65
|
+
config_path = File.expand_path(File.join(self.root, config_path)).to_s
|
66
|
+
config = self.load_config_for_env(config_path, self.env)
|
67
|
+
rescue
|
68
|
+
raise ConfigLoadingError, "Failed to load required config for environment '#{self.env}': #{config_path}"
|
69
|
+
end
|
70
|
+
|
71
|
+
# Merge required + optional keys.
|
72
|
+
keys_values = optional_keys.merge(required_keys)
|
73
|
+
|
74
|
+
# Stash found keys from the config into ENV.
|
75
|
+
keys_values.each do |extected_env_key, extected_yaml_key|
|
76
|
+
::Yaml2env::LOADED_ENV[extected_env_key.to_s] = ::ENV[extected_env_key.to_s] = config[extected_yaml_key.to_s]
|
77
|
+
self.logger.info ":: ENV[#{extected_env_key.inspect}] = #{::ENV[extected_env_key.to_s].inspect}" if self.logger?
|
78
|
+
end
|
79
|
+
|
80
|
+
# Raise error if any credentials are missing.
|
81
|
+
required_keys.keys.each do |env_key|
|
82
|
+
::Yaml2env::LOADED_ENV[env_key.to_s] ||
|
83
|
+
raise(MissingConfigKeyError, "ENV variable '#{env_key}' needs to be set. Query: #{keys_values.inspect}. Found: #{config.inspect}")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def detect_root!
|
88
|
+
self.root ||= if ::ENV.key?('RACK_ROOT')
|
89
|
+
::ENV['RACK_ROOT']
|
90
|
+
elsif defined?(::Rails)
|
91
|
+
::Rails.root
|
92
|
+
elsif defined?(::Sinatra::Application)
|
93
|
+
::Sinatra::Application.root
|
94
|
+
else
|
95
|
+
raise DetectionFailedError, "Failed to auto-detect Yaml.env (config environment). Specify environment before loading any configs/initializers using Yaml2env, e.g Yaml2env.env = 'development'."
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def detect_env!
|
100
|
+
self.env ||= if ::ENV.key?('RACK_ENV')
|
101
|
+
::ENV['RACK_ENV']
|
102
|
+
elsif defined?(::Rails)
|
103
|
+
::Rails.env
|
104
|
+
elsif defined?(::Sinatra::Application)
|
105
|
+
::Sinatra::Application.environment
|
106
|
+
else
|
107
|
+
raise DetectionFailedError, "Failed to auto-detect Yaml2env.root (config root). Specify environment before loading any configs/initializers using Yaml2env, e.g Yaml2env.env = 'development'."
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def logger?
|
112
|
+
self.logger.respond_to?(:info)
|
113
|
+
end
|
114
|
+
|
115
|
+
protected
|
116
|
+
def load_config(config_file)
|
117
|
+
YAML.load(File.open(config_file))
|
118
|
+
end
|
119
|
+
|
120
|
+
def load_config_for_env(config_file, env)
|
121
|
+
config = self.load_config(config_file)
|
122
|
+
config[env]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
development:
|
2
|
+
api_key: DEVELOPMENT_KEY
|
3
|
+
api_secret: DEVELOPMENT_SECRET
|
4
|
+
|
5
|
+
staging:
|
6
|
+
api_key: STAGING_KEY
|
7
|
+
api_secret: STAGING_SECRET
|
8
|
+
|
9
|
+
production:
|
10
|
+
api_key: PRODUCTION_KEY
|
11
|
+
api_secret: PRODUCTION_SECRET
|
12
|
+
|
13
|
+
test:
|
14
|
+
api_key: TEST_KEY
|
15
|
+
api_secret: TEST_SECRET
|
16
|
+
|
17
|
+
nyan_cat_mode:
|
18
|
+
api_key: NYAN_CAT_MODE_KEY
|
19
|
+
api_secret: NYAN_CAT_MODE_SECRET
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'minitest/unit'
|
4
|
+
require 'minitest/spec'
|
5
|
+
require 'minitest/pride'
|
6
|
+
require 'minitest/mock'
|
7
|
+
|
8
|
+
require 'yaml2env'
|
9
|
+
|
10
|
+
def silence_all_warnings
|
11
|
+
# Ruby 1.8: Kernel.silence_warnings { yield }
|
12
|
+
old_verbose = $VERBOSE
|
13
|
+
$VERBOSE = nil
|
14
|
+
yield
|
15
|
+
$VERBOSE = old_verbose
|
16
|
+
end
|
17
|
+
|
18
|
+
def with_constants(constants, &block)
|
19
|
+
saved_constants = {}
|
20
|
+
constants.each do |constant, value|
|
21
|
+
saved_constants[constant] = Object.const_get(constant)
|
22
|
+
silence_all_warnings do
|
23
|
+
Object.const_set(constant, value)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
block.call
|
29
|
+
ensure
|
30
|
+
constants.each do |constant, value|
|
31
|
+
silence_all_warnings do
|
32
|
+
Object.const_set(constant, saved_constants[constant])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def with_values(object, new_values)
|
39
|
+
old_values = {}
|
40
|
+
new_values.each do |key, value|
|
41
|
+
old_values[key] = object.send key
|
42
|
+
object.send :"#{key}=", value
|
43
|
+
end
|
44
|
+
yield
|
45
|
+
ensure
|
46
|
+
old_values.each do |key, value|
|
47
|
+
object.send :"#{key}=", value
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,351 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe Yaml2env do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@valid_config_filename = 'example.yml'
|
7
|
+
@valid_config_path = File.join(File.dirname(__FILE__), 'fixtures', @valid_config_filename)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "::VERSION" do
|
11
|
+
it 'should be defined' do
|
12
|
+
defined?(::Yaml2env::VERSION)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should be a valid version string (e.g. "0.0.1", or "0.0.1.rc1")' do
|
16
|
+
valid_version_string = /^\d+\.\d+\.\d+/
|
17
|
+
Yaml2env::VERSION.must_match valid_version_string
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "::LOADED_ENV" do
|
22
|
+
it 'should be defined' do
|
23
|
+
defined?(::Yaml2env::LOADED_ENV)
|
24
|
+
end
|
25
|
+
|
26
|
+
# it 'should be a empty hash' do
|
27
|
+
# Yaml2env::LOADED_ENV.must_equal({})
|
28
|
+
# end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".logger" do
|
32
|
+
before do
|
33
|
+
Yaml2env.defaults!
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should be defined' do
|
37
|
+
Yaml2env.must_respond_to :logger
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should be a valid logger' do
|
41
|
+
Yaml2env.logger.must_respond_to :info
|
42
|
+
Yaml2env.logger.must_respond_to :debug
|
43
|
+
Yaml2env.logger.must_respond_to :warn
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should be writable' do
|
47
|
+
class MyLogger < Logger
|
48
|
+
end
|
49
|
+
custom_logger = MyLogger.new(::STDOUT)
|
50
|
+
Yaml2env.logger = custom_logger
|
51
|
+
Yaml2env.logger.must_equal custom_logger
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe ".logger?" do
|
56
|
+
it 'should be defined' do
|
57
|
+
Yaml2env.must_respond_to :logger?
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should be a valid logger' do
|
61
|
+
Yaml2env.logger = nil
|
62
|
+
Yaml2env.logger?.must_equal false
|
63
|
+
|
64
|
+
Yaml2env.logger = Logger.new(::STDOUT)
|
65
|
+
Yaml2env.logger?.must_equal true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe ".root" do
|
70
|
+
it 'should be defined' do
|
71
|
+
Yaml2env.must_respond_to :root
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should be writable' do
|
75
|
+
Yaml2env.root = "/tmp"
|
76
|
+
Yaml2env.root.must_equal "/tmp"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe ".env" do
|
81
|
+
it 'should be defined' do
|
82
|
+
Yaml2env.must_respond_to :env
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should be writable' do
|
86
|
+
Yaml2env.root = "/tmp"
|
87
|
+
Yaml2env.root.must_equal "/tmp"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe ".configure" do
|
92
|
+
it 'should be defined' do
|
93
|
+
Yaml2env.must_respond_to :configure
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should be possible to change settings in a block' do
|
97
|
+
Yaml2env.root = '/tmp/hello_world'
|
98
|
+
Yaml2env.env = 'staging'
|
99
|
+
Yaml2env.configure do |c|
|
100
|
+
c.root = '/home/grimen/projects/hello_world'
|
101
|
+
c.env = 'production'
|
102
|
+
end
|
103
|
+
Yaml2env.root.must_equal '/home/grimen/projects/hello_world'
|
104
|
+
Yaml2env.env.must_equal 'production'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe ".detect_root!" do
|
109
|
+
it 'should be defined' do
|
110
|
+
Yaml2env.must_respond_to :detect_root!
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should detect environment for Rack-apps - 1st" do
|
114
|
+
rack!(true)
|
115
|
+
rails!(true)
|
116
|
+
sinatra!(true)
|
117
|
+
|
118
|
+
Yaml2env.root = nil
|
119
|
+
Yaml2env.detect_root!
|
120
|
+
Yaml2env.root.must_equal '/home/grimen/development/rack-app'
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should detect environment for Rails-apps - 2nd" do
|
124
|
+
rack!(false)
|
125
|
+
rails!(true)
|
126
|
+
sinatra!(true)
|
127
|
+
|
128
|
+
Yaml2env.root = nil
|
129
|
+
Yaml2env.detect_root!
|
130
|
+
Yaml2env.root.must_equal '/home/grimen/development/rails-app'
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should detect environment for Sinatra-apps - 3rd" do
|
134
|
+
rack!(false)
|
135
|
+
rails!(false)
|
136
|
+
sinatra!(true)
|
137
|
+
|
138
|
+
Yaml2env.root = nil
|
139
|
+
Yaml2env.detect_root!
|
140
|
+
Yaml2env.root.must_equal '/home/grimen/development/sinatra-app'
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'should complain if no environment could be detected' do
|
144
|
+
rack!(false)
|
145
|
+
rails!(false)
|
146
|
+
sinatra!(false)
|
147
|
+
|
148
|
+
Yaml2env.root = nil
|
149
|
+
assert_raises Yaml2env::DetectionFailedError do
|
150
|
+
Yaml2env.detect_root!
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe ".detect_env!" do
|
156
|
+
it 'should be defined' do
|
157
|
+
Yaml2env.must_respond_to :detect_env!
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should detect environment for Rack-apps - 1st" do
|
161
|
+
rack!(true)
|
162
|
+
rails!(true)
|
163
|
+
sinatra!(true)
|
164
|
+
|
165
|
+
Yaml2env.env = nil
|
166
|
+
Yaml2env.detect_env!
|
167
|
+
Yaml2env.env.must_equal 'rack-env'
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should detect environment for Rails-apps - 2nd" do
|
171
|
+
rack!(false)
|
172
|
+
rails!(true)
|
173
|
+
sinatra!(true)
|
174
|
+
|
175
|
+
Yaml2env.env = nil
|
176
|
+
Yaml2env.detect_env!
|
177
|
+
Yaml2env.env.must_equal 'rails-env'
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should detect environment for Sinatra-apps - 3rd" do
|
181
|
+
rack!(false)
|
182
|
+
rails!(false)
|
183
|
+
sinatra!(true)
|
184
|
+
|
185
|
+
Yaml2env.env = nil
|
186
|
+
Yaml2env.detect_env!
|
187
|
+
Yaml2env.env.must_equal 'sinatra-env'
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'should complain if no environment could be detected' do
|
191
|
+
rack!(false)
|
192
|
+
rails!(false)
|
193
|
+
sinatra!(false)
|
194
|
+
|
195
|
+
Yaml2env.env = nil
|
196
|
+
assert_raises Yaml2env::DetectionFailedError do
|
197
|
+
Yaml2env.detect_env!
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
describe "private/protected" do
|
203
|
+
describe ".load_config" do
|
204
|
+
it 'should be defined' do
|
205
|
+
Yaml2env.must_respond_to :load_config
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'should load config for all environments for a valid config YAML file without issues' do
|
209
|
+
assert (config = Yaml2env.send(:load_config, @valid_config_path))
|
210
|
+
config.must_be_kind_of Hash
|
211
|
+
config.must_equal({
|
212
|
+
"development" => {"api_key"=>"DEVELOPMENT_KEY", "api_secret"=>"DEVELOPMENT_SECRET"},
|
213
|
+
"test" => {"api_key"=>"TEST_KEY", "api_secret"=>"TEST_SECRET"},
|
214
|
+
"staging" => {"api_key"=>"STAGING_KEY", "api_secret"=>"STAGING_SECRET"},
|
215
|
+
"production" => {"api_key"=>"PRODUCTION_KEY", "api_secret"=>"PRODUCTION_SECRET"},
|
216
|
+
"nyan_cat_mode" => {"api_key"=>"NYAN_CAT_MODE_KEY", "api_secret"=>"NYAN_CAT_MODE_SECRET"}
|
217
|
+
})
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
describe ".load_config_for_env" do
|
222
|
+
it 'should be defined' do
|
223
|
+
Yaml2env.must_respond_to :load_config_for_env
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'should load config for a valid environment for a valid config YAML file without issues' do
|
227
|
+
env_config = Yaml2env.send(:load_config_for_env, @valid_config_path, 'development')
|
228
|
+
env_config.must_equal({
|
229
|
+
"api_key"=>"DEVELOPMENT_KEY",
|
230
|
+
"api_secret"=>"DEVELOPMENT_SECRET"
|
231
|
+
})
|
232
|
+
|
233
|
+
env_config = Yaml2env.send(:load_config_for_env, @valid_config_path, 'test')
|
234
|
+
env_config.must_equal({
|
235
|
+
"api_key"=>"TEST_KEY", "api_secret"=>"TEST_SECRET"
|
236
|
+
})
|
237
|
+
|
238
|
+
env_config = Yaml2env.send(:load_config_for_env, @valid_config_path, 'staging')
|
239
|
+
env_config.must_equal({
|
240
|
+
"api_key"=>"STAGING_KEY", "api_secret"=>"STAGING_SECRET"
|
241
|
+
})
|
242
|
+
|
243
|
+
env_config = Yaml2env.send(:load_config_for_env, @valid_config_path, 'production')
|
244
|
+
env_config.must_equal({
|
245
|
+
"api_key"=>"PRODUCTION_KEY",
|
246
|
+
"api_secret"=>"PRODUCTION_SECRET"
|
247
|
+
})
|
248
|
+
|
249
|
+
env_config = Yaml2env.send(:load_config_for_env, @valid_config_path, 'nyan_cat_mode')
|
250
|
+
env_config.must_equal({
|
251
|
+
"api_key"=>"NYAN_CAT_MODE_KEY",
|
252
|
+
"api_secret"=>"NYAN_CAT_MODE_SECRET"
|
253
|
+
})
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'should not load config for a missing environment for a valid config YAML file without issues' do
|
257
|
+
env_config = Yaml2env.send(:load_config_for_env, @valid_config_path, 'missing')
|
258
|
+
env_config.must_equal(nil)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe ".load" do
|
264
|
+
before do
|
265
|
+
Yaml2env.env = 'production'
|
266
|
+
Yaml2env.root = File.dirname(__FILE__)
|
267
|
+
Yaml2env.logger = nil
|
268
|
+
end
|
269
|
+
|
270
|
+
it 'should be defined' do
|
271
|
+
Yaml2env.must_respond_to :load
|
272
|
+
end
|
273
|
+
|
274
|
+
it 'should throw error if specified config file that do not exist' do
|
275
|
+
assert_raises Yaml2env::ConfigLoadingError do
|
276
|
+
Yaml2env.load 'null.yml'
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
it 'should not throw error if specified config file do exist' do
|
281
|
+
assert Yaml2env.load('fixtures/example.yml')
|
282
|
+
end
|
283
|
+
|
284
|
+
it 'should throw error if a specified constant-key do not exist in the config file' do
|
285
|
+
assert_raises Yaml2env::MissingConfigKeyError do
|
286
|
+
Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'bla'}
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'should not throw error if a specified constant-key do in fact exist in the config file' do
|
291
|
+
assert Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
|
292
|
+
end
|
293
|
+
|
294
|
+
it 'should set - with Yaml2env - loaded ENV-values' do
|
295
|
+
Yaml2env::LOADED_ENV = {}
|
296
|
+
Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
|
297
|
+
Yaml2env::LOADED_ENV.must_equal({"API_SECRET" => "PRODUCTION_SECRET", "API_KEY" => "PRODUCTION_KEY"})
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
protected
|
302
|
+
|
303
|
+
def rack!(loaded)
|
304
|
+
if loaded
|
305
|
+
::ENV['RACK_ROOT'] = '/home/grimen/development/rack-app'
|
306
|
+
::ENV['RACK_ENV'] = 'rack-env'
|
307
|
+
else
|
308
|
+
::ENV['RACK_ROOT'] = nil
|
309
|
+
::ENV['RACK_ENV'] = nil
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
def rails!(loaded)
|
314
|
+
if loaded
|
315
|
+
eval <<-EVAL
|
316
|
+
unless defined?(::Rails)
|
317
|
+
module Rails
|
318
|
+
class << self
|
319
|
+
attr_accessor :root, :env
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
EVAL
|
324
|
+
Rails.root = '/home/grimen/development/rails-app'
|
325
|
+
Rails.env = 'rails-env'
|
326
|
+
else
|
327
|
+
Object.send(:remove_const, :Rails) if defined?(::Rails)
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
def sinatra!(loaded)
|
332
|
+
if loaded
|
333
|
+
eval <<-EVAL
|
334
|
+
unless defined?(::Sinatra::Application)
|
335
|
+
module Sinatra
|
336
|
+
class Application
|
337
|
+
class << self
|
338
|
+
attr_accessor :root, :environment
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|
343
|
+
EVAL
|
344
|
+
Sinatra::Application.root = '/home/grimen/development/sinatra-app'
|
345
|
+
Sinatra::Application.environment = 'sinatra-env'
|
346
|
+
else
|
347
|
+
Object.send(:remove_const, :Sinatra) if defined?(::Sinatra::Application)
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
end
|
data/yaml2env.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "yaml2env/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "yaml2env"
|
7
|
+
s.version = Yaml2env::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Merchii", "Jonas Grimfelt"]
|
10
|
+
s.email = ["jonas@merchii.com", "grimen@gmail.com"]
|
11
|
+
s.homepage = "http://github.com/merchii/yaml2env"
|
12
|
+
s.summary = %q{YAML => ENV for environment-specific configs}
|
13
|
+
s.description = %q{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).}
|
14
|
+
|
15
|
+
s.required_rubygems_version = '>= 1.3.6'
|
16
|
+
s.rubyforge_project = s.name
|
17
|
+
|
18
|
+
s.add_development_dependency 'bundler', '~> 1.0.0'
|
19
|
+
s.add_development_dependency 'minitest'
|
20
|
+
s.add_development_dependency 'guard'
|
21
|
+
s.add_development_dependency 'guard-bundler'
|
22
|
+
s.add_development_dependency 'guard-minitest'
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split("\n")
|
25
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
26
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
27
|
+
s.require_paths = ["lib"]
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml2env
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Merchii
|
14
|
+
- Jonas Grimfelt
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-07-25 00:00:00 +02:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: bundler
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 23
|
31
|
+
segments:
|
32
|
+
- 1
|
33
|
+
- 0
|
34
|
+
- 0
|
35
|
+
version: 1.0.0
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: minitest
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
type: :development
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: guard
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: guard-bundler
|
68
|
+
prerelease: false
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
type: :development
|
79
|
+
version_requirements: *id004
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: guard-minitest
|
82
|
+
prerelease: false
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
type: :development
|
93
|
+
version_requirements: *id005
|
94
|
+
description: 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).
|
95
|
+
email:
|
96
|
+
- jonas@merchii.com
|
97
|
+
- grimen@gmail.com
|
98
|
+
executables: []
|
99
|
+
|
100
|
+
extensions: []
|
101
|
+
|
102
|
+
extra_rdoc_files: []
|
103
|
+
|
104
|
+
files:
|
105
|
+
- .gitignore
|
106
|
+
- Gemfile
|
107
|
+
- Guardfile
|
108
|
+
- README.textile
|
109
|
+
- Rakefile
|
110
|
+
- lib/yaml2env.rb
|
111
|
+
- lib/yaml2env/version.rb
|
112
|
+
- spec/fixtures/example.yml
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
- spec/yaml2env_spec.rb
|
115
|
+
- yaml2env.gemspec
|
116
|
+
has_rdoc: true
|
117
|
+
homepage: http://github.com/merchii/yaml2env
|
118
|
+
licenses: []
|
119
|
+
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
hash: 3
|
131
|
+
segments:
|
132
|
+
- 0
|
133
|
+
version: "0"
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
hash: 23
|
140
|
+
segments:
|
141
|
+
- 1
|
142
|
+
- 3
|
143
|
+
- 6
|
144
|
+
version: 1.3.6
|
145
|
+
requirements: []
|
146
|
+
|
147
|
+
rubyforge_project: yaml2env
|
148
|
+
rubygems_version: 1.5.0
|
149
|
+
signing_key:
|
150
|
+
specification_version: 3
|
151
|
+
summary: YAML => ENV for environment-specific configs
|
152
|
+
test_files:
|
153
|
+
- spec/fixtures/example.yml
|
154
|
+
- spec/spec_helper.rb
|
155
|
+
- spec/yaml2env_spec.rb
|