layeredyamlconfig 1.4.3

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.
@@ -0,0 +1,20 @@
1
+ # adapted from Ruby Facets (https://github.com/rubyworks/facets) to support nested Array traversal
2
+ # we're using the Ruby license, clause 2a by making LayeredYAMLConfig freely available
3
+ class Hash
4
+
5
+ def traverse(&blk)
6
+ nh = {}
7
+ inject(nh) do |h, (k, v)|
8
+ case v
9
+ when Hash
10
+ v = v.traverse(&blk)
11
+ when Array
12
+ v = v.traverse(&blk)
13
+ end
14
+ nv = blk.call(v)
15
+ h[k.to_sym] = nv
16
+ h
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,64 @@
1
+ require 'layeredyamlconfig/array_traverse.rb'
2
+ require 'layeredyamlconfig/hash_traverse.rb'
3
+
4
+ class LayeredYAMLConfig
5
+
6
+ def resolve_templates
7
+ # lazy-load dependencies
8
+ if ! defined?(TinyEruby)
9
+ require 'erubis/tiny'
10
+ end
11
+
12
+ # if templates are enabled:
13
+ # - walk the tree finding String leaves that look like ERB tags
14
+ # - evaluate them with self as context
15
+ # - rescue errors and increment success / failure counts
16
+ # - A: if we saw no failures, we're done
17
+ # - B: if we saw no successes for the first time, set a flag
18
+ # if we saw no failures for the second time, raise
19
+ # - otherwise keep going until A or B are true
20
+ all_failed_once = false
21
+ while true do
22
+ success = 0
23
+ failure = 0
24
+ newcfg = @cfg.traverse do |v|
25
+ if v.kind_of?(String) and v.match(/^<%=/)
26
+ begin
27
+ nv = Erubis::TinyEruby.new(v).evaluate(:cfg => @cfg)
28
+ if ! self.class.emptyok && nv.empty?
29
+ failure += 1
30
+ v
31
+ elsif ! nv.match(/^<%=/)
32
+ success += 1
33
+ nv
34
+ else
35
+ failure += 1
36
+ v
37
+ end
38
+ rescue
39
+ failure += 1
40
+ v
41
+ end
42
+ else
43
+ v
44
+ end
45
+ end
46
+ @cfg = newcfg
47
+ # all success
48
+ if 0 == failure
49
+ return
50
+ # all failure
51
+ elsif 0 == success
52
+ if all_failed_once
53
+ raise "all template evaluation failed"
54
+ else
55
+ all_failed_once = true
56
+ end
57
+ # mix
58
+ else
59
+ all_failed_once = false
60
+ end
61
+ end
62
+ end
63
+
64
+ end
data/test/ex1.yaml ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ foo: bar
data/test/ex10.yaml ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ foo: bar
3
+ baz: <%= @cfg[:foo] %>
data/test/ex11.yaml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ foo:
3
+ bar:
4
+ baz: <%= @cfg[:foo][:bar][:quux] %>
data/test/ex12.yaml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ foo:
3
+ bar:
4
+ quux: gzonk
data/test/ex13.yaml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ a:
3
+ b:
4
+ c: <%= @cfg[:d][:e][:f] %>
5
+
data/test/ex14.yaml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ d:
3
+ e:
4
+ f: <%= @cfg[:g][:h][:i] %>
data/test/ex15.yaml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ g:
3
+ h:
4
+ i: j
data/test/ex16.yaml ADDED
@@ -0,0 +1,8 @@
1
+ ---
2
+ a: d
3
+ b: e
4
+ c: f
5
+ g:
6
+ - <%= @cfg[:a] %>
7
+ - <%= @cfg[:b] %>
8
+ - <%= @cfg[:c] %>
data/test/ex17.yaml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ a:
3
+ b:
4
+ c: d
5
+ e: <%= @cfg[:a][:b][:f] %>
data/test/ex18.yaml ADDED
@@ -0,0 +1,23 @@
1
+ ---
2
+ a:
3
+ - 1
4
+ - 2
5
+ - 3
6
+ b:
7
+ one:
8
+ a: <%= @cfg[:a][0] %>
9
+ two:
10
+ b: <%= @cfg[:a][1] %>
11
+ three:
12
+ c: <%= @cfg[:a][2] %>
13
+ c:
14
+ - - <%= @cfg[:a][0] %>
15
+ - <%= @cfg[:a][1] %>
16
+ - <%= @cfg[:a][2] %>
17
+ - - <%= @cfg[:a][0] %>
18
+ - <%= @cfg[:a][1] %>
19
+ - <%= @cfg[:a][2] %>
20
+ d:
21
+ - one: <%= @cfg[:a][0] %>
22
+ two: <%= @cfg[:a][1] %>
23
+ three: <%= @cfg[:a][2] %>
data/test/ex2.yaml ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ bar: baz
data/test/ex3.yaml ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ foo: bar
data/test/ex4.yaml ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ foo: baz
data/test/ex5.yaml ADDED
@@ -0,0 +1,6 @@
1
+ # this is a comment
2
+ foo: bar
3
+ # this is a comment for a list
4
+ bar: [ baz, quux ]
5
+ # this is a comment for a map
6
+ baz: { foo: bar, baz: quux }
data/test/ex6.yaml ADDED
@@ -0,0 +1,9 @@
1
+ foo:
2
+ # a comment
3
+ - bar
4
+ # another comment
5
+ - baz
6
+ bar: # map comment
7
+ bar: baz
8
+ # a comment
9
+ quux: gzonk
data/test/ex7.yaml ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ foo:
3
+ bar: baz
data/test/ex8.yaml ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ foo:
3
+ gzonk: quux
data/test/ex9.yaml ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ foo:
3
+ wibble: wobble
data/test/exbad1.yaml ADDED
@@ -0,0 +1 @@
1
+ foo
data/test/exbad2.yaml ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ - foo
@@ -0,0 +1,12 @@
1
+ require 'simplecov'
2
+ require 'simplecov-console'
3
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
+ SimpleCov::Formatter::HTMLFormatter,
5
+ SimpleCov::Formatter::Console,
6
+ ]
7
+ SimpleCov.start do
8
+ add_filter '/vendor/'
9
+ add_filter '/test/'
10
+ end
11
+
12
+ require 'minitest/debugger' if ENV['DEBUG']
@@ -0,0 +1,22 @@
1
+ require 'minitest_helper'
2
+ require 'minitest/autorun'
3
+ require 'layeredyamlconfig'
4
+
5
+ class OurConfig < LayeredYAMLConfig
6
+ end
7
+
8
+ class TestAddLayer < Minitest::Test
9
+ def setup
10
+ LayeredYAMLConfig.clear_all
11
+ LayeredYAMLConfig.reset_all
12
+ end
13
+ def test_add_layer
14
+ c = OurConfig.instance( 'test/ex7.yaml', 'test/ex8.yaml' )
15
+ assert_instance_of(OurConfig, c)
16
+ assert_equal('baz', c[:foo][:bar])
17
+ assert_equal('quux', c[:foo][:gzonk])
18
+ c.add( 'test/ex9.yaml' )
19
+ assert_instance_of(OurConfig, c)
20
+ assert_equal('wobble', c[:foo]['wibble'])
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+ require 'minitest_helper'
2
+ require 'minitest/autorun'
3
+ require 'layeredyamlconfig'
4
+
5
+ class OurConfig1 < LayeredYAMLConfig
6
+ end
7
+
8
+ class OurConfig2 < LayeredYAMLConfig
9
+ end
10
+
11
+ class TestClear < Minitest::Test
12
+ def setup
13
+ LayeredYAMLConfig.clear_all
14
+ LayeredYAMLConfig.reset_all
15
+ end
16
+ def test_clear_oneclass
17
+ c1 = OurConfig1.instance( 'test/ex1.yaml' )
18
+ c2 = OurConfig2.instance( 'test/ex1.yaml' )
19
+ assert_instance_of(OurConfig1, c1)
20
+ assert_instance_of(OurConfig2, c2)
21
+ assert_raises(ArgumentError) {
22
+ OurConfig1.instance( 'test/ex1.yaml' )
23
+ }
24
+ OurConfig1.clear
25
+ c3 = OurConfig1.instance( 'test/ex1.yaml' )
26
+ assert_instance_of(OurConfig1, c3)
27
+ c4 = OurConfig2.instance
28
+ assert_instance_of(OurConfig2, c4)
29
+ assert_equal(c2, c4)
30
+ end
31
+ def test_clear_all
32
+ c1 = OurConfig1.instance( 'test/ex1.yaml' )
33
+ c2 = OurConfig2.instance( 'test/ex1.yaml' )
34
+ assert_instance_of(OurConfig1, c1)
35
+ assert_instance_of(OurConfig2, c2)
36
+ assert_raises(ArgumentError) {
37
+ OurConfig1.instance( 'test/ex1.yaml' )
38
+ }
39
+ assert_raises(ArgumentError) {
40
+ OurConfig2.instance( 'test/ex1.yaml' )
41
+ }
42
+ LayeredYAMLConfig.clear_all
43
+ c3 = OurConfig1.instance( 'test/ex1.yaml' )
44
+ assert_instance_of(OurConfig1, c3)
45
+ c4 = OurConfig2.instance( 'test/ex1.yaml' )
46
+ assert_instance_of(OurConfig2, c4)
47
+ end
48
+ end
@@ -0,0 +1,26 @@
1
+ require 'minitest_helper'
2
+ require 'minitest/autorun'
3
+ require 'layeredyamlconfig'
4
+
5
+ class OurConfig < LayeredYAMLConfig
6
+ end
7
+
8
+ class TestComments < Minitest::Test
9
+ def setup
10
+ LayeredYAMLConfig.clear_all
11
+ LayeredYAMLConfig.reset_all
12
+ end
13
+ def test_file_with_comments
14
+ c = OurConfig.instance( 'test/ex5.yaml' )
15
+ assert_instance_of(OurConfig, c)
16
+ assert_equal('bar', c[:foo])
17
+ assert_equal(%w(baz quux), c[:bar])
18
+ assert_equal({ 'foo' => 'bar', 'baz' => 'quux' }, c["baz"])
19
+ end
20
+ def test_embedded_comments
21
+ c = OurConfig.instance( 'test/ex6.yaml' )
22
+ assert_instance_of(OurConfig, c)
23
+ assert_equal(%w(bar baz), c[:foo])
24
+ assert_equal({ 'bar' => 'baz', 'quux' => 'gzonk' }, c["bar"])
25
+ end
26
+ end
@@ -0,0 +1,50 @@
1
+ require 'minitest_helper'
2
+ require 'minitest/autorun'
3
+ require 'layeredyamlconfig'
4
+
5
+ class OurConfig < LayeredYAMLConfig
6
+ end
7
+
8
+ class TestConstructor < Minitest::Test
9
+ def setup
10
+ OurConfig.clear
11
+ OurConfig.reset
12
+ end
13
+ def test_nofiles
14
+ assert_raises(ArgumentError) {
15
+ OurConfig.instance
16
+ }
17
+ end
18
+ def test_onefile
19
+ c = OurConfig.instance( 'test/ex1.yaml' )
20
+ assert_instance_of(OurConfig, c)
21
+ assert_equal('bar', c[:foo])
22
+ assert_equal('bar', c['foo'])
23
+ end
24
+ def test_onefile_noarray
25
+ c = OurConfig.instance( 'test/ex1.yaml' )
26
+ assert_instance_of(OurConfig, c)
27
+ assert_equal('bar', c[:foo])
28
+ assert_equal('bar', c['foo'])
29
+ end
30
+ def test_twofiles
31
+ c = OurConfig.instance( 'test/ex1.yaml', 'test/ex2.yaml' )
32
+ assert_instance_of(OurConfig, c)
33
+ assert_equal('bar', c[:foo])
34
+ assert_equal('bar', c['foo'])
35
+ assert_equal('baz', c[:bar])
36
+ assert_equal('baz', c['bar'])
37
+ end
38
+ def test_twofiles_3_4
39
+ c = OurConfig.instance( 'test/ex3.yaml', 'test/ex4.yaml' )
40
+ assert_instance_of(OurConfig, c)
41
+ assert_equal('baz', c[:foo])
42
+ assert_equal('baz', c['foo'])
43
+ end
44
+ def test_twofiles_4_3
45
+ c = OurConfig.instance( 'test/ex4.yaml', 'test/ex3.yaml' )
46
+ assert_instance_of(OurConfig, c)
47
+ assert_equal('bar', c[:foo])
48
+ assert_equal('bar', c['foo'])
49
+ end
50
+ end
@@ -0,0 +1,19 @@
1
+ require 'minitest_helper'
2
+ require 'minitest/autorun'
3
+ require 'layeredyamlconfig'
4
+
5
+ class OurConfig < LayeredYAMLConfig
6
+ end
7
+
8
+ class TestDeepMerge < Minitest::Test
9
+ def setup
10
+ LayeredYAMLConfig.clear_all
11
+ LayeredYAMLConfig.reset_all
12
+ end
13
+ def test_deep_merge
14
+ c = OurConfig.instance( 'test/ex7.yaml', 'test/ex8.yaml' )
15
+ assert_instance_of(OurConfig, c)
16
+ assert_equal('baz', c[:foo][:bar])
17
+ assert_equal('quux', c[:foo][:gzonk])
18
+ end
19
+ end
data/test/test_erb.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'minitest_helper'
2
+ require 'minitest/autorun'
3
+ require 'layeredyamlconfig'
4
+
5
+ class OurConfig < LayeredYAMLConfig
6
+ end
7
+
8
+ class TestErb < Minitest::Test
9
+ def setup
10
+ OurConfig.clear
11
+ OurConfig.reset
12
+ end
13
+ def test_erb_disabled
14
+ c = OurConfig.instance( 'test/ex10.yaml' )
15
+ assert_instance_of(OurConfig, c)
16
+ assert_match '<%=', c[:baz]
17
+ end
18
+ def test_erb_enabled
19
+ OurConfig.templates = true
20
+ c = OurConfig.instance( 'test/ex10.yaml' )
21
+ assert_instance_of(OurConfig, c)
22
+ assert_equal 'bar', c[:baz]
23
+ end
24
+ end
@@ -0,0 +1,40 @@
1
+ require 'minitest_helper'
2
+ require 'minitest/autorun'
3
+ require 'layeredyamlconfig'
4
+
5
+ class OurConfig < LayeredYAMLConfig
6
+ end
7
+
8
+ class TestErbArray < Minitest::Test
9
+ def setup
10
+ OurConfig.clear
11
+ OurConfig.reset
12
+ end
13
+ def test_erb_array
14
+ OurConfig.templates = true
15
+ c = OurConfig.instance 'test/ex16.yaml'
16
+ assert_instance_of(OurConfig, c)
17
+ assert_equal 'd', c[:g][0]
18
+ assert_equal 'e', c[:g][1]
19
+ assert_equal 'f', c[:g][2]
20
+ end
21
+ def test_erb_array_of_array
22
+ OurConfig.templates = true
23
+ c = OurConfig.instance 'test/ex18.yaml'
24
+ assert_instance_of(OurConfig, c)
25
+ assert_equal '1', c[:c][0][0]
26
+ assert_equal '2', c[:c][0][1]
27
+ assert_equal '3', c[:c][0][2]
28
+ assert_equal '1', c[:c][1][0]
29
+ assert_equal '2', c[:c][1][1]
30
+ assert_equal '3', c[:c][1][2]
31
+ end
32
+ def test_erb_array_of_hash
33
+ OurConfig.templates = true
34
+ c = OurConfig.instance 'test/ex18.yaml'
35
+ assert_instance_of(OurConfig, c)
36
+ assert_equal '1', c[:d][0][:one]
37
+ assert_equal '2', c[:d][0][:two]
38
+ assert_equal '3', c[:d][0][:three]
39
+ end
40
+ end