yaml2env 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +5 -2
- data/Gemfile +7 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +25 -7
- data/lib/yaml2env/version.rb +1 -1
- data/lib/yaml2env.rb +18 -3
- data/spec/yaml2env_spec.rb +76 -5
- metadata +13 -11
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Jonas Grimfelt, Merchii. http://merchii.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
CHANGED
@@ -25,7 +25,10 @@ h2. Usage
|
|
25
25
|
To give this some context; this is how we use @Yaml2Env@ to initialize "Hoptoad":http://hoptoadapp.com:
|
26
26
|
|
27
27
|
<pre>
|
28
|
-
Yaml2Env.load 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
28
|
+
Yaml2Env.load! 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
29
|
+
|
30
|
+
# ...or if a warning note in the logs is enough:
|
31
|
+
# Yaml2Env.load 'config/hoptoad.yml', {'HOPTOAD_API_KEY' => 'api_key'}
|
29
32
|
|
30
33
|
if defined?(HoptoadNotifier)
|
31
34
|
HoptoadNotifier.configure do |config|
|
@@ -53,22 +56,37 @@ To give this some context; this is how we use @Yaml2Env@ to initialize "Hoptoad"
|
|
53
56
|
...which will yield:
|
54
57
|
|
55
58
|
<pre>
|
56
|
-
Rails.env
|
59
|
+
# Rails.env => 'development'
|
57
60
|
ENV['HOPTOAD_API_KEY'] => 'NONE'
|
58
61
|
|
59
|
-
Rails.env
|
62
|
+
# Rails.env => 'staging'
|
60
63
|
ENV['HOPTOAD_API_KEY'] => '123abc'
|
61
64
|
|
62
|
-
Rails.env
|
65
|
+
# Rails.env => 'production'
|
63
66
|
ENV['HOPTOAD_API_KEY'] => 'abc123'
|
64
67
|
|
65
|
-
Rails.env
|
68
|
+
# Rails.env => 'test'
|
66
69
|
ENV['HOPTOAD_API_KEY'] => 'NONE'
|
67
70
|
|
68
|
-
Rails.env
|
71
|
+
# Rails.env => 'other'
|
69
72
|
=> "Failed to load required config for environment 'other': /Users/grimen/development/example.com/config/hoptoad.yml"
|
70
73
|
</pre>
|
71
74
|
|
75
|
+
h2. Helpers
|
76
|
+
|
77
|
+
A few convenient helpers:
|
78
|
+
|
79
|
+
<pre>
|
80
|
+
Yaml2Env.loaded? 'HOPTOAD_API_KEY'
|
81
|
+
=> true
|
82
|
+
|
83
|
+
Yaml2Env.loaded? 'BAZOOKA'
|
84
|
+
=> false
|
85
|
+
|
86
|
+
Yaml2Env.loaded? 'HOPTOAD_API_KEY', 'BAZOOKA'
|
87
|
+
=> false
|
88
|
+
</pre>
|
89
|
+
|
72
90
|
h2. Notes
|
73
91
|
|
74
92
|
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.
|
@@ -76,5 +94,5 @@ This gem was developed for our own requirements at *"Merchii":http://github.com/
|
|
76
94
|
h2. License
|
77
95
|
|
78
96
|
Released under the MIT license.
|
79
|
-
Copyright (c) "
|
97
|
+
Copyright (c) "Jonas Grimfelt":http://github.com/grimen, "Merchii":http://github.com/merchii
|
80
98
|
|
data/lib/yaml2env/version.rb
CHANGED
data/lib/yaml2env.rb
CHANGED
@@ -55,7 +55,7 @@ module Yaml2env
|
|
55
55
|
yield self
|
56
56
|
end
|
57
57
|
|
58
|
-
def load(config_path, required_keys = {}, optional_keys = {})
|
58
|
+
def load!(config_path, required_keys = {}, optional_keys = {})
|
59
59
|
self.detect_root!
|
60
60
|
self.detect_env!
|
61
61
|
|
@@ -84,6 +84,21 @@ module Yaml2env
|
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
|
+
def load(config_path, required_keys = {}, optional_keys = {})
|
88
|
+
begin
|
89
|
+
self.load!(config_path, required_keys, optional_keys)
|
90
|
+
rescue Error => e
|
91
|
+
if self.logger?
|
92
|
+
::Yaml2env.logger.warn("[Yaml2env]: #{e} -- called from: #{__FILE__})")
|
93
|
+
end
|
94
|
+
end
|
95
|
+
true
|
96
|
+
end
|
97
|
+
|
98
|
+
def loaded?(*constant_names)
|
99
|
+
constant_names.all? { |cn| ::Yaml2env::LOADED_ENV.key?(cn.to_s) }
|
100
|
+
end
|
101
|
+
|
87
102
|
def detect_root!
|
88
103
|
self.root ||= if ::ENV.key?('RACK_ROOT')
|
89
104
|
::ENV['RACK_ROOT']
|
@@ -92,7 +107,7 @@ module Yaml2env
|
|
92
107
|
elsif defined?(::Sinatra::Application)
|
93
108
|
::Sinatra::Application.root
|
94
109
|
else
|
95
|
-
raise DetectionFailedError, "Failed to auto-detect Yaml.
|
110
|
+
raise DetectionFailedError, "Failed to auto-detect Yaml.root (config root). Specify root before loading any configs/initializers using Yaml2env, e.g. Yaml2env.root = '~/projects/my_app'."
|
96
111
|
end
|
97
112
|
end
|
98
113
|
|
@@ -104,7 +119,7 @@ module Yaml2env
|
|
104
119
|
elsif defined?(::Sinatra::Application)
|
105
120
|
::Sinatra::Application.environment
|
106
121
|
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'."
|
122
|
+
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
123
|
end
|
109
124
|
end
|
110
125
|
|
data/spec/yaml2env_spec.rb
CHANGED
@@ -260,7 +260,7 @@ describe Yaml2env do
|
|
260
260
|
end
|
261
261
|
end
|
262
262
|
|
263
|
-
describe ".load" do
|
263
|
+
describe ".load!" do
|
264
264
|
before do
|
265
265
|
Yaml2env.env = 'production'
|
266
266
|
Yaml2env.root = File.dirname(__FILE__)
|
@@ -268,26 +268,60 @@ describe Yaml2env do
|
|
268
268
|
end
|
269
269
|
|
270
270
|
it 'should be defined' do
|
271
|
-
Yaml2env.must_respond_to :load
|
271
|
+
Yaml2env.must_respond_to :load!
|
272
272
|
end
|
273
273
|
|
274
274
|
it 'should throw error if specified config file that do not exist' do
|
275
275
|
assert_raises Yaml2env::ConfigLoadingError do
|
276
|
-
Yaml2env.load 'null.yml'
|
276
|
+
Yaml2env.load! 'null.yml'
|
277
277
|
end
|
278
278
|
end
|
279
279
|
|
280
280
|
it 'should not throw error if specified config file do exist' do
|
281
|
-
assert Yaml2env.load('fixtures/example.yml')
|
281
|
+
assert Yaml2env.load!('fixtures/example.yml')
|
282
282
|
end
|
283
283
|
|
284
284
|
it 'should throw error if a specified constant-key do not exist in the config file' do
|
285
285
|
assert_raises Yaml2env::MissingConfigKeyError do
|
286
|
-
Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'bla'}
|
286
|
+
Yaml2env.load! 'fixtures/example.yml', {:API_KEY => 'bla'}
|
287
287
|
end
|
288
288
|
end
|
289
289
|
|
290
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.clear unless Yaml2env::LOADED_ENV.empty?
|
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
|
+
describe ".load" do
|
302
|
+
before do
|
303
|
+
Yaml2env.env = 'production'
|
304
|
+
Yaml2env.root = File.dirname(__FILE__)
|
305
|
+
Yaml2env.logger = nil
|
306
|
+
end
|
307
|
+
|
308
|
+
it 'should be defined' do
|
309
|
+
Yaml2env.must_respond_to :load
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'should at maximum log warning if specified config file that do not exist' do
|
313
|
+
assert Yaml2env.load('null.yml')
|
314
|
+
end
|
315
|
+
|
316
|
+
it 'should not log warning or raise error if specified config file do exist' do
|
317
|
+
assert Yaml2env.load('fixtures/example.yml')
|
318
|
+
end
|
319
|
+
|
320
|
+
it 'should at maximum log warning if a specified constant-key do not exist in the config file' do
|
321
|
+
assert Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'bla'}
|
322
|
+
end
|
323
|
+
|
324
|
+
it 'should not log warning or raise error if a specified constant-key do in fact exist in the config file' do
|
291
325
|
assert Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
|
292
326
|
end
|
293
327
|
|
@@ -298,6 +332,43 @@ describe Yaml2env do
|
|
298
332
|
end
|
299
333
|
end
|
300
334
|
|
335
|
+
describe ".loaded?" do
|
336
|
+
before do
|
337
|
+
Yaml2env::LOADED_ENV.clear unless Yaml2env::LOADED_ENV.empty?
|
338
|
+
end
|
339
|
+
|
340
|
+
it 'should be defined' do
|
341
|
+
Yaml2env.must_respond_to :loaded?
|
342
|
+
end
|
343
|
+
|
344
|
+
describe "one argument" do
|
345
|
+
it 'should return false if specified constant is not loaded into ENV' do
|
346
|
+
Yaml2env.loaded?('API_KEY').must_equal false
|
347
|
+
end
|
348
|
+
|
349
|
+
it 'should return true if specified constant is loaded into ENV' do
|
350
|
+
Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
|
351
|
+
Yaml2env.loaded?('API_KEY').must_equal true
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
describe "multiple arguments" do
|
356
|
+
it 'should return false if none of the specified constants are not loaded into ENV' do
|
357
|
+
Yaml2env.loaded?('API_KEY', 'API_SECRET').must_equal false
|
358
|
+
end
|
359
|
+
|
360
|
+
it 'should return false if any of the specified constants are not loaded into ENV' do
|
361
|
+
Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'api_key'}
|
362
|
+
Yaml2env.loaded?('API_KEY', 'API_SECRET').must_equal false
|
363
|
+
end
|
364
|
+
|
365
|
+
it 'should return true if all specified constants are loaded into ENV' do
|
366
|
+
Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
|
367
|
+
Yaml2env.loaded?('API_KEY', 'API_SECRET').must_equal true
|
368
|
+
end
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
301
372
|
protected
|
302
373
|
|
303
374
|
def rack!(loaded)
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: yaml2env
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Merchii
|
@@ -11,12 +11,10 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-
|
15
|
-
default_executable:
|
14
|
+
date: 2011-08-20 00:00:00 Z
|
16
15
|
dependencies:
|
17
16
|
- !ruby/object:Gem::Dependency
|
18
17
|
name: rake
|
19
|
-
prerelease: false
|
20
18
|
requirement: &id001 !ruby/object:Gem::Requirement
|
21
19
|
none: false
|
22
20
|
requirements:
|
@@ -24,10 +22,10 @@ dependencies:
|
|
24
22
|
- !ruby/object:Gem::Version
|
25
23
|
version: "0"
|
26
24
|
type: :development
|
25
|
+
prerelease: false
|
27
26
|
version_requirements: *id001
|
28
27
|
- !ruby/object:Gem::Dependency
|
29
28
|
name: bundler
|
30
|
-
prerelease: false
|
31
29
|
requirement: &id002 !ruby/object:Gem::Requirement
|
32
30
|
none: false
|
33
31
|
requirements:
|
@@ -35,10 +33,10 @@ dependencies:
|
|
35
33
|
- !ruby/object:Gem::Version
|
36
34
|
version: 1.0.0
|
37
35
|
type: :development
|
36
|
+
prerelease: false
|
38
37
|
version_requirements: *id002
|
39
38
|
- !ruby/object:Gem::Dependency
|
40
39
|
name: minitest
|
41
|
-
prerelease: false
|
42
40
|
requirement: &id003 !ruby/object:Gem::Requirement
|
43
41
|
none: false
|
44
42
|
requirements:
|
@@ -46,10 +44,10 @@ dependencies:
|
|
46
44
|
- !ruby/object:Gem::Version
|
47
45
|
version: "0"
|
48
46
|
type: :development
|
47
|
+
prerelease: false
|
49
48
|
version_requirements: *id003
|
50
49
|
- !ruby/object:Gem::Dependency
|
51
50
|
name: guard
|
52
|
-
prerelease: false
|
53
51
|
requirement: &id004 !ruby/object:Gem::Requirement
|
54
52
|
none: false
|
55
53
|
requirements:
|
@@ -57,10 +55,10 @@ dependencies:
|
|
57
55
|
- !ruby/object:Gem::Version
|
58
56
|
version: "0"
|
59
57
|
type: :development
|
58
|
+
prerelease: false
|
60
59
|
version_requirements: *id004
|
61
60
|
- !ruby/object:Gem::Dependency
|
62
61
|
name: guard-bundler
|
63
|
-
prerelease: false
|
64
62
|
requirement: &id005 !ruby/object:Gem::Requirement
|
65
63
|
none: false
|
66
64
|
requirements:
|
@@ -68,10 +66,10 @@ dependencies:
|
|
68
66
|
- !ruby/object:Gem::Version
|
69
67
|
version: "0"
|
70
68
|
type: :development
|
69
|
+
prerelease: false
|
71
70
|
version_requirements: *id005
|
72
71
|
- !ruby/object:Gem::Dependency
|
73
72
|
name: guard-minitest
|
74
|
-
prerelease: false
|
75
73
|
requirement: &id006 !ruby/object:Gem::Requirement
|
76
74
|
none: false
|
77
75
|
requirements:
|
@@ -79,6 +77,7 @@ dependencies:
|
|
79
77
|
- !ruby/object:Gem::Version
|
80
78
|
version: "0"
|
81
79
|
type: :development
|
80
|
+
prerelease: false
|
82
81
|
version_requirements: *id006
|
83
82
|
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).
|
84
83
|
email:
|
@@ -95,6 +94,7 @@ files:
|
|
95
94
|
- .travis.yml
|
96
95
|
- Gemfile
|
97
96
|
- Guardfile
|
97
|
+
- MIT-LICENSE
|
98
98
|
- README.textile
|
99
99
|
- Rakefile
|
100
100
|
- lib/yaml2env.rb
|
@@ -103,7 +103,6 @@ files:
|
|
103
103
|
- spec/spec_helper.rb
|
104
104
|
- spec/yaml2env_spec.rb
|
105
105
|
- yaml2env.gemspec
|
106
|
-
has_rdoc: true
|
107
106
|
homepage: http://github.com/merchii/yaml2env
|
108
107
|
licenses: []
|
109
108
|
|
@@ -117,6 +116,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
117
116
|
requirements:
|
118
117
|
- - ">="
|
119
118
|
- !ruby/object:Gem::Version
|
119
|
+
hash: -148891887331939387
|
120
|
+
segments:
|
121
|
+
- 0
|
120
122
|
version: "0"
|
121
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
124
|
none: false
|
@@ -127,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
129
|
requirements: []
|
128
130
|
|
129
131
|
rubyforge_project: yaml2env
|
130
|
-
rubygems_version: 1.
|
132
|
+
rubygems_version: 1.8.7
|
131
133
|
signing_key:
|
132
134
|
specification_version: 3
|
133
135
|
summary: YAML => ENV for environment-specific configs
|