rbatch 2.5.0 → 2.5.1
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/README.ja.md +1 -1
- data/README.md +1 -1
- data/bin/rbatch-init +1 -1
- data/lib/rbatch/config.rb +33 -18
- data/lib/rbatch/version.rb +1 -1
- data/sample/.rbatchrc +1 -1
- data/spec/rbatch/config_spec.rb +24 -5
- metadata +2 -2
data/README.ja.md
CHANGED
data/README.md
CHANGED
data/bin/rbatch-init
CHANGED
data/lib/rbatch/config.rb
CHANGED
@@ -8,19 +8,19 @@ module RBatch
|
|
8
8
|
@path
|
9
9
|
|
10
10
|
# Actual data
|
11
|
-
@
|
11
|
+
@element
|
12
12
|
|
13
13
|
# @param [String] path Config file path
|
14
14
|
def initialize(path,is_erb = false)
|
15
15
|
@path = path
|
16
16
|
begin
|
17
17
|
if is_erb
|
18
|
-
@
|
18
|
+
@element = Config.parse(YAML::load(ERB.new(IO.read(@path)).result))
|
19
19
|
else
|
20
|
-
@
|
20
|
+
@element = Config.parse(YAML::load_file(@path))
|
21
21
|
end
|
22
22
|
rescue Errno::ENOENT => e
|
23
|
-
@
|
23
|
+
@element = nil
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -28,10 +28,10 @@ module RBatch
|
|
28
28
|
# @param [Object] key Config key.
|
29
29
|
# @raise [RBatch::ConfigException]
|
30
30
|
def[](key)
|
31
|
-
if @
|
31
|
+
if @element.nil?
|
32
32
|
raise RBatch::ConfigException, "Config file \"#{@path}\" does not exist"
|
33
33
|
else
|
34
|
-
@
|
34
|
+
@element[key]
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -41,36 +41,51 @@ module RBatch
|
|
41
41
|
|
42
42
|
# Config file exists or not
|
43
43
|
# @return [Boolean]
|
44
|
-
def exist? ; ! @
|
44
|
+
def exist? ; ! @element.nil? ; end
|
45
45
|
|
46
46
|
# @return [Hash]
|
47
47
|
def to_h
|
48
|
-
if @
|
48
|
+
if @element.nil?
|
49
49
|
raise RBatch::ConfigException, "Config file \"#{@path}\" does not exist"
|
50
50
|
else
|
51
|
-
@
|
51
|
+
@element
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
55
|
# @return [String]
|
56
56
|
def to_s
|
57
|
-
if @
|
57
|
+
if @element.nil?
|
58
58
|
raise RBatch::ConfigException, "Config file \"#{@path}\" does not exist"
|
59
59
|
else
|
60
|
-
@
|
60
|
+
@element.to_s
|
61
61
|
end
|
62
62
|
end
|
63
|
-
end
|
64
63
|
|
65
|
-
|
64
|
+
# @return ConfigElementArray or ConfigElementHash
|
65
|
+
def Config.parse(yaml)
|
66
|
+
if yaml.class == Hash
|
67
|
+
return ConfigElementHash.new(yaml)
|
68
|
+
elsif yaml.class == Array
|
69
|
+
return ConfigElementArray.new(yaml)
|
70
|
+
else
|
71
|
+
return yaml
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class ConfigElementArray < Array
|
77
|
+
def initialize(array)
|
78
|
+
array.each_with_index do |item,index|
|
79
|
+
self[index] = Config.parse(item)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class ConfigElementHash < Hash
|
66
85
|
def initialize(hash)
|
67
86
|
if hash
|
68
87
|
hash.each_key do |key|
|
69
|
-
|
70
|
-
self[key] = ConfigElement.new(hash[key])
|
71
|
-
else
|
72
|
-
self[key] = hash[key]
|
73
|
-
end
|
88
|
+
self[key] = Config.parse(hash[key])
|
74
89
|
end
|
75
90
|
end
|
76
91
|
end
|
data/lib/rbatch/version.rb
CHANGED
data/sample/.rbatchrc
CHANGED
data/spec/rbatch/config_spec.rb
CHANGED
@@ -77,8 +77,8 @@ describe RBatch::Config do
|
|
77
77
|
|
78
78
|
end
|
79
79
|
|
80
|
-
describe RBatch::
|
81
|
-
it "" do
|
80
|
+
describe "RBatch::Config.parse" do
|
81
|
+
it "parses hash" do
|
82
82
|
hash = {
|
83
83
|
"a" => "av" ,
|
84
84
|
"b" => {
|
@@ -89,11 +89,11 @@ describe RBatch::ConfigElement do
|
|
89
89
|
}
|
90
90
|
}
|
91
91
|
}
|
92
|
-
ce = RBatch::
|
92
|
+
ce = RBatch::Config.parse(hash)
|
93
93
|
expect(ce["a"]).to eq "av"
|
94
|
-
expect(ce["b"].class).to eq RBatch::
|
94
|
+
expect(ce["b"].class).to eq RBatch::ConfigElementHash
|
95
95
|
expect(ce["b"]["c"]).to eq "cv"
|
96
|
-
expect(ce["b"]["d"].class).to eq RBatch::
|
96
|
+
expect(ce["b"]["d"].class).to eq RBatch::ConfigElementHash
|
97
97
|
expect(ce["b"]["d"]["e"]).to eq "ev"
|
98
98
|
expect(ce["b"]["d"]["f"][1]).to eq 2
|
99
99
|
expect { ce["noexist"] }.to raise_error(RBatch::ConfigException)
|
@@ -101,4 +101,23 @@ describe RBatch::ConfigElement do
|
|
101
101
|
expect { ce["b"]["noexist"] }.to raise_error(RBatch::ConfigException)
|
102
102
|
expect { ce["b"]["d"]["noexist"] }.to raise_error(RBatch::ConfigException)
|
103
103
|
end
|
104
|
+
|
105
|
+
it "parses array" do
|
106
|
+
array = [
|
107
|
+
"a",
|
108
|
+
[ "b", "c" ],
|
109
|
+
{ "d" => "dv" , "e" => "ev" },
|
110
|
+
[ "f",["g","h","i"]]
|
111
|
+
]
|
112
|
+
ce = RBatch::Config.parse(array)
|
113
|
+
expect(ce.class).to eq RBatch::ConfigElementArray
|
114
|
+
expect(ce[0]).to eq "a"
|
115
|
+
expect(ce[1].class).to eq RBatch::ConfigElementArray
|
116
|
+
expect(ce[1][0]).to eq "b"
|
117
|
+
expect(ce[1][1]).to eq "c"
|
118
|
+
expect(ce[2].class).to eq RBatch::ConfigElementHash
|
119
|
+
expect(ce[2]["d"]).to eq "dv"
|
120
|
+
expect { ce[2]["z"] }.to raise_error(RBatch::ConfigException)
|
121
|
+
expect(ce[3][1][1]).to eq "h"
|
122
|
+
end
|
104
123
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rbatch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 2.5.
|
5
|
+
version: 2.5.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- fetaro
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2015-06-
|
13
|
+
date: 2015-06-17 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: Batch Script Framework
|