configtoolkit 1.0.6 → 1.0.7
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/History.txt +3 -0
- data/Manifest.txt +2 -2
- data/Rakefile +1 -1
- data/lib/configtoolkit/baseconfig.rb +298 -234
- data/lib/configtoolkit/types.rb +13 -0
- data/lib/configtoolkit/yamlreader.rb +22 -0
- data/lib/configtoolkit/yamlwriter.rb +106 -27
- data/test/{sample.ruby_yaml_types.yaml → sample.ruby_yaml_classes.yaml} +0 -0
- data/test/{sample.standard_yaml_types.yaml → sample.standard_yaml_classes.yaml} +0 -0
- data/test/test_yaml.rb +14 -14
- metadata +3 -3
data/lib/configtoolkit/types.rb
CHANGED
@@ -40,8 +40,21 @@ class ConstrainedArray
|
|
40
40
|
# store the constraints.
|
41
41
|
#
|
42
42
|
class << self
|
43
|
+
#
|
44
|
+
# The class of all of the elements.
|
45
|
+
#
|
43
46
|
attr_reader :element_class
|
47
|
+
|
48
|
+
#
|
49
|
+
# The minimum number of elements, or nil if there is no
|
50
|
+
# minimum.
|
51
|
+
#
|
44
52
|
attr_reader :min_num_elements
|
53
|
+
|
54
|
+
#
|
55
|
+
# The maximum number of elements, or nil if there is no
|
56
|
+
# maximum.
|
57
|
+
#
|
45
58
|
attr_reader :max_num_elements
|
46
59
|
end
|
47
60
|
|
@@ -3,7 +3,24 @@ require 'yaml'
|
|
3
3
|
|
4
4
|
module ConfigToolkit
|
5
5
|
|
6
|
+
#
|
7
|
+
# This class implements the reader interface for
|
8
|
+
# YAML configuration files. It basically is a wrapper
|
9
|
+
# over Ruby's YAML library.
|
10
|
+
#
|
6
11
|
class YAMLReader
|
12
|
+
#
|
13
|
+
# ====Description:
|
14
|
+
# This constructs a YAMLReader instance for stream,
|
15
|
+
# where stream either is a file name (a String)
|
16
|
+
# or an IO object.
|
17
|
+
#
|
18
|
+
# ====Parameters:
|
19
|
+
# [stream]
|
20
|
+
# A file name (String) or an IO object. If
|
21
|
+
# stream is a file name, then the YAMLReader
|
22
|
+
# will open the associated file.
|
23
|
+
#
|
7
24
|
def initialize(stream)
|
8
25
|
if(stream.class == String)
|
9
26
|
@stream = File.open(stream, "r")
|
@@ -12,6 +29,11 @@ class YAMLReader
|
|
12
29
|
end
|
13
30
|
end
|
14
31
|
|
32
|
+
#
|
33
|
+
# ====Returns:
|
34
|
+
# the contents of the next YAML document read from the underlying
|
35
|
+
# stream.
|
36
|
+
#
|
15
37
|
def read
|
16
38
|
return YAML::load(@stream)
|
17
39
|
end
|
@@ -3,57 +3,136 @@ require 'yaml'
|
|
3
3
|
|
4
4
|
module ConfigToolkit
|
5
5
|
|
6
|
+
#
|
7
|
+
# This class implements the writer interface for
|
8
|
+
# YAML configuration files. It basically is a wrapper
|
9
|
+
# over Ruby's YAML library.
|
10
|
+
#
|
11
|
+
# YAML natively supports Integers, Floats, Strings, and Booleans.
|
12
|
+
# Instances of other classes are written by Ruby to YAML configuration
|
13
|
+
# files by recursively writing each of the instance's variables and
|
14
|
+
# noting in the YAML file the associated Ruby classes. This may not
|
15
|
+
# be desired behavior for a configuration file, especially if the file
|
16
|
+
# is going to be shared with a non-Ruby program. On the other hand,
|
17
|
+
# if only Ruby is using the configuration the file, this behavior may
|
18
|
+
# well be very desirable (since arbitrarily complex objects can be
|
19
|
+
# supported properly by the YAML format). Thus, the YAMLWriter
|
20
|
+
# offers two options:
|
21
|
+
# * Only standard YAML classes will be written. Values of classes
|
22
|
+
# not supported natively by YAML will be converted to Strings.
|
23
|
+
# * Non-standard YAML classes will be written.
|
24
|
+
#
|
6
25
|
class YAMLWriter
|
7
|
-
|
26
|
+
#
|
27
|
+
# ====Description:
|
28
|
+
# This constructs a YAMLWriter instance for stream,
|
29
|
+
# where stream either is a file name (a String)
|
30
|
+
# or an IO object. If only_output_standard_yaml_classes, then
|
31
|
+
# the writer only will output standard YAML classes; all classes not
|
32
|
+
# native to YAML will be converted into Strings before being
|
33
|
+
# written. If !only_output_standard_yaml_classes, however, then
|
34
|
+
# Ruby serialized classes will be written to the configuration
|
35
|
+
# file.
|
36
|
+
#
|
37
|
+
# ====Parameters:
|
38
|
+
# [stream]
|
39
|
+
# A file name (String) or an IO object. If
|
40
|
+
# stream is a file name, then the YAMLReader
|
41
|
+
# will open the associated file.
|
42
|
+
#
|
43
|
+
# [only_output_standard_yaml_classes]
|
44
|
+
# Whether or not the writer should output only standard
|
45
|
+
# YAML classes.
|
46
|
+
#
|
47
|
+
def initialize(stream, only_output_standard_yaml_classes)
|
8
48
|
if(stream.class == String)
|
9
49
|
@stream = File.open(stream, "w")
|
10
50
|
else
|
11
51
|
@stream = stream
|
12
52
|
end
|
13
53
|
|
14
|
-
@
|
54
|
+
@only_output_standard_yaml_classes = only_output_standard_yaml_classes
|
15
55
|
end
|
16
56
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
57
|
+
#
|
58
|
+
# ====Description:
|
59
|
+
# This method converts value to an equivalent representation
|
60
|
+
# in a standard YAML class. In particular,
|
61
|
+
# * Hash eleemnts are converted with a
|
62
|
+
# convert_hash_value_to_standard_yaml_classes call
|
63
|
+
# * Arrays elements are converted with recursive
|
64
|
+
# convert_value_to_standard_yaml_class calls
|
65
|
+
# * Non-standard YAML classes are converted to Strings (via to_s)
|
66
|
+
#
|
67
|
+
# ====Parameters:
|
68
|
+
# [value]
|
69
|
+
# The value to convert
|
70
|
+
#
|
71
|
+
# ====Returns:
|
72
|
+
# An equivalent representation of value in a standard YAML class
|
73
|
+
#
|
74
|
+
def convert_value_to_standard_yaml_class(value)
|
75
|
+
if(value.class == Hash)
|
76
|
+
return convert_hash_values_to_standard_yaml_classes(value)
|
77
|
+
elsif(value.class == Array)
|
78
|
+
new_value = []
|
79
|
+
|
80
|
+
value.each do |element|
|
81
|
+
new_value.push(convert_value_to_standard_yaml_class(element))
|
25
82
|
end
|
26
83
|
|
27
|
-
return
|
28
|
-
elsif((
|
29
|
-
(
|
30
|
-
(
|
31
|
-
(
|
32
|
-
(
|
33
|
-
return
|
84
|
+
return new_value
|
85
|
+
elsif((value.class <= Integer) ||
|
86
|
+
(value.class <= Float) ||
|
87
|
+
(value.class == TrueClass) ||
|
88
|
+
(value.class == FalseClass) ||
|
89
|
+
(value.class == String))
|
90
|
+
return value
|
34
91
|
else
|
35
|
-
return
|
92
|
+
return value.to_s
|
36
93
|
end
|
37
94
|
end
|
95
|
+
private :convert_value_to_standard_yaml_class
|
38
96
|
|
39
|
-
|
40
|
-
|
97
|
+
#
|
98
|
+
# ====Description:
|
99
|
+
# This method converts all elements of hash_value to standard
|
100
|
+
# YAML classes via convert_value_to_standard_yaml_class calls.
|
101
|
+
#
|
102
|
+
# ====Parameters:
|
103
|
+
# [hash_value]
|
104
|
+
# The Hash to convert
|
105
|
+
#
|
106
|
+
# ====Returns:
|
107
|
+
# An equivalent Hash, the values of which are of standard YAML classes.
|
108
|
+
#
|
109
|
+
def convert_hash_values_to_standard_yaml_classes(hash_value)
|
110
|
+
standard_yaml_class_hash_value = {}
|
41
111
|
|
42
|
-
|
43
|
-
|
112
|
+
hash_value.each do |param_name, value|
|
113
|
+
standard_yaml_class_hash_value[param_name] = convert_value_to_standard_yaml_class(value)
|
44
114
|
end
|
45
115
|
|
46
|
-
return
|
116
|
+
return standard_yaml_class_hash_value
|
47
117
|
end
|
118
|
+
private :convert_hash_values_to_standard_yaml_classes
|
48
119
|
|
120
|
+
#
|
121
|
+
# ====Description:
|
122
|
+
# This method writes config_hash to the underlying stream.
|
123
|
+
#
|
124
|
+
# ====Parameters:
|
125
|
+
# [config_hash]
|
126
|
+
# The configuration hash to write
|
127
|
+
#
|
49
128
|
def write(config_hash)
|
50
|
-
if(@
|
51
|
-
|
129
|
+
if(@only_output_standard_yaml_classes)
|
130
|
+
output_hash = convert_hash_values_to_standard_yaml_classes(config_hash)
|
52
131
|
else
|
53
|
-
|
132
|
+
output_hash = config_hash
|
54
133
|
end
|
55
134
|
|
56
|
-
YAML::dump(
|
135
|
+
YAML::dump(output_hash, @stream)
|
57
136
|
@stream.flush()
|
58
137
|
end
|
59
138
|
end
|
File without changes
|
File without changes
|
data/test/test_yaml.rb
CHANGED
@@ -13,17 +13,17 @@ class YAMLTest < Test::Unit::TestCase
|
|
13
13
|
run_reader_test(ConfigToolkit::YAMLReader,
|
14
14
|
"",
|
15
15
|
REFERENCE_SAMPLE_CONFIG,
|
16
|
-
get_test_config_file_name("sample.
|
16
|
+
get_test_config_file_name("sample.standard_yaml_classes.yaml"))
|
17
17
|
|
18
18
|
run_reader_test(ConfigToolkit::YAMLReader,
|
19
19
|
"",
|
20
20
|
REFERENCE_SAMPLE_CONFIG,
|
21
|
-
get_test_config_file_name("sample.
|
21
|
+
get_test_config_file_name("sample.ruby_yaml_classes.yaml"))
|
22
22
|
|
23
23
|
run_reader_test(ConfigToolkit::YAMLReader,
|
24
24
|
"",
|
25
25
|
REFERENCE_SAMPLE_CONFIG,
|
26
|
-
File.new(get_test_config_file_name("sample.
|
26
|
+
File.new(get_test_config_file_name("sample.ruby_yaml_classes.yaml"), "r"))
|
27
27
|
|
28
28
|
run_reader_test(ConfigToolkit::YAMLReader,
|
29
29
|
"contained_sample",
|
@@ -34,23 +34,23 @@ class YAMLTest < Test::Unit::TestCase
|
|
34
34
|
def test_writers
|
35
35
|
run_writer_test(ConfigToolkit::YAMLWriter,
|
36
36
|
REFERENCE_SAMPLE_CONFIG,
|
37
|
-
get_test_config_file_name("sample.
|
38
|
-
get_test_config_file_name("sample.
|
39
|
-
get_test_config_file_name("sample.
|
37
|
+
get_test_config_file_name("sample.standard_yaml_classes.yaml"),
|
38
|
+
get_test_config_file_name("sample.standard_yaml_classes.dumped.yaml"),
|
39
|
+
get_test_config_file_name("sample.standard_yaml_classes.dumped.yaml"),
|
40
40
|
true)
|
41
41
|
|
42
42
|
run_writer_test(ConfigToolkit::YAMLWriter,
|
43
43
|
REFERENCE_SAMPLE_CONFIG,
|
44
|
-
get_test_config_file_name("sample.
|
45
|
-
get_test_config_file_name("sample.
|
46
|
-
get_test_config_file_name("sample.
|
44
|
+
get_test_config_file_name("sample.ruby_yaml_classes.yaml"),
|
45
|
+
get_test_config_file_name("sample.ruby_yaml_classes.dumped.yaml"),
|
46
|
+
get_test_config_file_name("sample.ruby_yaml_classes.dumped.yaml"),
|
47
47
|
false)
|
48
48
|
|
49
49
|
run_writer_test(ConfigToolkit::YAMLWriter,
|
50
50
|
REFERENCE_SAMPLE_CONFIG,
|
51
|
-
get_test_config_file_name("sample.
|
52
|
-
get_test_config_file_name("sample.
|
53
|
-
File.new(get_test_config_file_name("sample.
|
51
|
+
get_test_config_file_name("sample.standard_yaml_classes.yaml"),
|
52
|
+
get_test_config_file_name("sample.standard_yaml_classes.dumped.yaml"),
|
53
|
+
File.new(get_test_config_file_name("sample.standard_yaml_classes.dumped.yaml"), "w"),
|
54
54
|
true)
|
55
55
|
|
56
56
|
run_writer_test(ConfigToolkit::YAMLWriter,
|
@@ -65,8 +65,8 @@ class YAMLTest < Test::Unit::TestCase
|
|
65
65
|
run_reader_writer_test(ConfigToolkit::YAMLReader,
|
66
66
|
ConfigToolkit::YAMLWriter,
|
67
67
|
REFERENCE_SAMPLE_CONFIG,
|
68
|
-
[get_test_config_file_name("sample.
|
69
|
-
[get_test_config_file_name("sample.
|
68
|
+
[get_test_config_file_name("sample.standard_yaml_classes.dumped.yaml")],
|
69
|
+
[get_test_config_file_name("sample.standard_yaml_classes.dumped.yaml"), false])
|
70
70
|
run_reader_writer_test(ConfigToolkit::YAMLReader,
|
71
71
|
ConfigToolkit::YAMLWriter,
|
72
72
|
REFERENCE_CONTAINED_SAMPLE_CONFIG,
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configtoolkit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DesigningPatterns
|
@@ -52,8 +52,8 @@ files:
|
|
52
52
|
- test/contained_sample.yaml
|
53
53
|
- test/firewall.yaml
|
54
54
|
- test/machines.yaml
|
55
|
-
- test/sample.
|
56
|
-
- test/sample.
|
55
|
+
- test/sample.ruby_yaml_classes.yaml
|
56
|
+
- test/sample.standard_yaml_classes.yaml
|
57
57
|
- test/webserver.yaml
|
58
58
|
has_rdoc: true
|
59
59
|
homepage: http://configtoolkit.rubyforge.org/
|