configtoolkit 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/FAQ.txt +5 -3
- data/Hash.txt +4 -2
- data/History.txt +18 -0
- data/KeyValue.txt +6 -4
- data/Manifest.txt +4 -0
- data/Override.txt +49 -42
- data/README.txt +122 -11
- data/Rakefile +1 -1
- data/Ruby.txt +4 -2
- data/YAML.txt +4 -2
- data/examples/config_toolkit_config_example.rb +61 -0
- data/examples/config_toolkit_config_example.yaml +6 -0
- data/examples/override_example_1.rb +9 -4
- data/examples/override_example_2.rb +3 -3
- data/lib/configtoolkit/baseconfig.rb +119 -8
- data/lib/configtoolkit/configtoolkitconfig.rb +77 -0
- data/test/firewall.yaml +1 -0
- data/test/test_baseconfig.rb +97 -0
- data/test/test_configtoolkitconfig.rb +27 -0
- metadata +9 -4
data/FAQ.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
|
2
|
+
|
1
3
|
=== Why can't I use a normal Ruby Array rather than a ConstrainedArray in my configuration's specification?
|
2
4
|
You can, but you probably don't want to. Actually, underneath, the
|
3
5
|
ConfigToolkit converts Array parameters into ConfigToolkit::ConstrainedArray
|
@@ -86,7 +88,7 @@ all cases, except:
|
|
86
88
|
made available. In this case, a key-value format is best, but the format
|
87
89
|
cannot use key-value extensions like Hashes, Arrays, or include directives
|
88
90
|
(since writing support for these in a key-value implementation in another
|
89
|
-
|
91
|
+
language is non-trivial)
|
90
92
|
|
91
93
|
=== How can configuration parameters be documented with rdoc?
|
92
94
|
Unfortunately, +rdoc+ cannot document configuration parameters by default. One
|
@@ -101,7 +103,7 @@ versions 2.0.0 and 2.1.0, which can be obtained with +gem+ or at
|
|
101
103
|
http://rubyforge.org/projects/rdoc/) that fixes this for
|
102
104
|
ConfigToolkit::BaseConfig.add_required_param and
|
103
105
|
ConfigToolkit::BaseConfig.add_optional_param:
|
104
|
-
|
106
|
+
======<tt>rdoc_support/rdoc-2.0.0.patch</tt>:
|
105
107
|
--- parse_rb.rb 2008-07-14 12:25:06.000000000 -0400
|
106
108
|
+++ /usr/lib/ruby/gems/1.8/gems/rdoc-2.0.0/lib/rdoc/parsers/parse_rb.rb 2008-07-14 12:32:02.000000000 -0400
|
107
109
|
@@ -2505,6 +2505,21 @@
|
@@ -129,7 +131,7 @@ ConfigToolkit::BaseConfig.add_optional_param:
|
|
129
131
|
|
130
132
|
This patch only should be applied to the +rdoc+ version 2.0.0 gem with the
|
131
133
|
+patch+ utility:
|
132
|
-
|
134
|
+
======<tt>rdoc_support/rdoc-2.1.0.patch</tt>:
|
133
135
|
--- rdoc-2.1.0/lib/rdoc/parser/ruby.rb 2008-07-25 13:09:44.000000000 -0400
|
134
136
|
+++ /usr/lib/ruby/gems/1.8/gems/rdoc-2.1.0/lib/rdoc/parser/ruby.rb 2008-07-25 13:08:37.000000000 -0400
|
135
137
|
@@ -1807,6 +1807,21 @@
|
data/Hash.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
|
2
|
+
|
1
3
|
== LOADING AND DUMPING CONFIGURATIONS FROM AND TO HASHES:
|
2
4
|
Reader classes extending the ConfigToolkit::Reader interface implement
|
3
5
|
a +read+ method (see ConfigToolkit::Reader#read) that reads configuration
|
@@ -12,7 +14,7 @@ useful in that it allows ConfigToolkit::BaseConfig#load to be run on data
|
|
12
14
|
specified programatically in a Hash.
|
13
15
|
|
14
16
|
== EXAMPLE:
|
15
|
-
|
17
|
+
======<tt>examples/hash_example.rb</tt>:
|
16
18
|
#!/usr/bin/env ruby
|
17
19
|
|
18
20
|
#
|
@@ -125,4 +127,4 @@ When run, the program produces:
|
|
125
127
|
}
|
126
128
|
|
127
129
|
The Hash dumped from the second config:
|
128
|
-
{:production=>{:www=>{:addresses=>[#<URI::HTTP:
|
130
|
+
{:production=>{:www=>{:addresses=>[#<URI::HTTP:0xb7cc042c URL:http://www.designingpatterns.com>, #<URI::HTTP:0xb7cc03dc URL:http://tokyo.designingpatterns.com>], :num_cpus=>64, :os=>{:version=>10.0, :name=>"Solaris"}, :behind_firewall=>true, :contains_sensitive_data=>true}}}
|
data/History.txt
CHANGED
@@ -1,3 +1,21 @@
|
|
1
|
+
=== 2.3.0 / 2009-06-09
|
2
|
+
* The ConfigToolkit will print out a warning when it encounters an
|
3
|
+
unknown parameter when loading a configuration file
|
4
|
+
(ConfigToolkit::BaseConfig#load and ConfigToolkit::BaseConfig#load_group).
|
5
|
+
This will make it easier to catch errors in configuration files. Previously,
|
6
|
+
such unknown parameters were ignored silently (thanks for the idea, Inaki).
|
7
|
+
* The ConfigToolkit now *itself* can be configured.
|
8
|
+
ConfigToolkit::ConfigToolkitConfig configurations can be specified for a
|
9
|
+
ConfigToolkit::BaseConfig child class or for an instance of a
|
10
|
+
ConfigToolkit::BaseConfig child class. The
|
11
|
+
ConfigToolkit::ConfigToolkitConfig allows customization of the loading
|
12
|
+
and dumping processes. Right now, it supports modifying the ConfigToolkit's
|
13
|
+
behavior upon encountering an unknown configuration parameter: the
|
14
|
+
ConfigToolkit can ignore the parameter (the behavior in prior versions),
|
15
|
+
warn about the parameter (the new default behavior), or raise a
|
16
|
+
ConfigToolkit::Error about the parameter. See README.txt for
|
17
|
+
more details.
|
18
|
+
|
1
19
|
=== 2.2.0 / 2008-07-29
|
2
20
|
* Add support for overriding configurations through the
|
3
21
|
ConfigToolkit::OverrideReader, allowing the contents of one configuration file
|
data/KeyValue.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
|
2
|
+
|
1
3
|
== KEY-VALUE CONFIGURATION FILES:
|
2
4
|
Key-value configuration files are quite common, although they come in a
|
3
5
|
myriad of different formats. +/etc/sysctl.conf+ on my system (CentOS 5.1), for
|
@@ -41,7 +43,7 @@ a new file).
|
|
41
43
|
== EXAMPLE:
|
42
44
|
The following configuration file contains one configuration in a common
|
43
45
|
key-value format (the ConfigToolkit's default key-value format, in fact):
|
44
|
-
|
46
|
+
======<tt>examples/key_value_example.normal1.cfg</tt>:
|
45
47
|
#
|
46
48
|
# This file contains MachineConfig configurations
|
47
49
|
# (see examples/machineconfig.rb for the configuration's specification).
|
@@ -65,7 +67,7 @@ key-value format (the ConfigToolkit's default key-value format, in fact):
|
|
65
67
|
|
66
68
|
It includes a second key-value configuration file containing another
|
67
69
|
configuration (in the same key-value format):
|
68
|
-
|
70
|
+
======<tt>examples/key_value_example.normal2.cfg</tt>:
|
69
71
|
#
|
70
72
|
# This file contains MachineConfig configurations
|
71
73
|
# (see examples/machineconfig.rb for the configuration's specification).
|
@@ -88,7 +90,7 @@ configuration also is represented with a different key-value
|
|
88
90
|
format (the format uses ":" instead of "=" to separate keys and
|
89
91
|
values, "<<-->>" instead of "=>" to separate hash keys and values, and
|
90
92
|
";" instead of "#" to prefix comments) in this file:
|
91
|
-
|
93
|
+
======<tt>examples/key_value_example.wacky.cfg</tt>:
|
92
94
|
;
|
93
95
|
; This file contains MachineConfig configurations
|
94
96
|
; (see examples/machineconfig.rb for the configuration's specification).
|
@@ -105,7 +107,7 @@ values, "<<-->>" instead of "=>" to separate hash keys and values, and
|
|
105
107
|
|
106
108
|
The following program loads these configurations and writes the second
|
107
109
|
configuration in a new key-value format:
|
108
|
-
|
110
|
+
======<tt>examples/key_value_example.rb</tt>:
|
109
111
|
#!/usr/bin/env ruby
|
110
112
|
|
111
113
|
#
|
data/Manifest.txt
CHANGED
@@ -9,6 +9,7 @@ README.txt
|
|
9
9
|
Ruby.txt
|
10
10
|
YAML.txt
|
11
11
|
Rakefile
|
12
|
+
examples/config_toolkit_config_example.rb
|
12
13
|
examples/hash_example.rb
|
13
14
|
examples/key_value_example.rb
|
14
15
|
examples/load_example.rb
|
@@ -19,6 +20,7 @@ examples/override_example_2.rb
|
|
19
20
|
examples/ruby_example.rb
|
20
21
|
examples/usage_example.rb
|
21
22
|
examples/yaml_example.rb
|
23
|
+
examples/config_toolkit_config_example.yaml
|
22
24
|
examples/load_example.yaml
|
23
25
|
examples/load_group_example.yaml
|
24
26
|
examples/override_example_override.yaml
|
@@ -32,6 +34,7 @@ examples/key_value_example.wacky.cfg
|
|
32
34
|
examples/ruby_example.rcfg
|
33
35
|
lib/configtoolkit.rb
|
34
36
|
lib/configtoolkit/baseconfig.rb
|
37
|
+
lib/configtoolkit/configtoolkitconfig.rb
|
35
38
|
lib/configtoolkit/hasharrayvisitor.rb
|
36
39
|
lib/configtoolkit/hashreader.rb
|
37
40
|
lib/configtoolkit/hashwriter.rb
|
@@ -50,6 +53,7 @@ rdoc_support/rdoc-2.0.0.patch
|
|
50
53
|
rdoc_support/rdoc-2.1.0.patch
|
51
54
|
test/readerwritertest.rb
|
52
55
|
test/test_baseconfig.rb
|
56
|
+
test/test_configtoolkitconfig.rb
|
53
57
|
test/test_hash.rb
|
54
58
|
test/test_keyvalue.rb
|
55
59
|
test/test_override.rb
|
data/Override.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
|
2
|
+
|
1
3
|
== OVERRIDE CONFIGURATION FILES:
|
2
4
|
Do you find yourself needing to override specific configuration
|
3
5
|
parameters in your configuration? For example, does your application
|
@@ -17,7 +19,7 @@ For more specific details see the ConfigToolkit::OverrideReader class.
|
|
17
19
|
Following are two YAML configurations that will be passed to the
|
18
20
|
override reader. Configuration in the second file will override that
|
19
21
|
of the first.
|
20
|
-
|
22
|
+
======<tt>examples/override_example.yaml</tt>:
|
21
23
|
#
|
22
24
|
# This file contains MachineConfig configurations
|
23
25
|
# (see examples/machineconfig.rb for the configuration's specification).
|
@@ -33,7 +35,7 @@ of the first.
|
|
33
35
|
- http://default.designingpatterns.com
|
34
36
|
- http://apple.designingpatterns.com
|
35
37
|
|
36
|
-
|
38
|
+
======<tt>examples/override_example_override.yaml</tt>:
|
37
39
|
#
|
38
40
|
# This file contains a subset of a MachineConfig configuration and
|
39
41
|
# is meant to be an override of the MachineConfig specified in
|
@@ -45,7 +47,7 @@ of the first.
|
|
45
47
|
version: 5.4
|
46
48
|
|
47
49
|
This is the program that reads the two above configurations and performs the override.
|
48
|
-
|
50
|
+
======<tt>examples/override_example_1.rb</tt>:
|
49
51
|
#!/usr/bin/env ruby
|
50
52
|
|
51
53
|
#
|
@@ -64,11 +66,16 @@ This is the program that reads the two above configurations and performs the ove
|
|
64
66
|
require 'configtoolkit/yamlreader'
|
65
67
|
require 'configtoolkit/overridereader'
|
66
68
|
|
67
|
-
CONFIGURATION_FILE =
|
68
|
-
|
69
|
+
CONFIGURATION_FILE =
|
70
|
+
File.expand_path_relative_to_caller("override_example.yaml")
|
71
|
+
OVERRIDE_FILE =
|
72
|
+
File.expand_path_relative_to_caller("override_example_override.yaml")
|
69
73
|
|
70
|
-
|
71
|
-
|
74
|
+
config_file_reader = ConfigToolkit::YAMLReader.new(CONFIGURATION_FILE)
|
75
|
+
override_file_reader = ConfigToolkit::YAMLReader.new(OVERRIDE_FILE)
|
76
|
+
|
77
|
+
reader = ConfigToolkit::OverrideReader.new(config_file_reader,
|
78
|
+
override_file_reader)
|
72
79
|
|
73
80
|
# Let's load the overriden configuration.
|
74
81
|
config = MachineConfig.load(reader)
|
@@ -78,17 +85,17 @@ When run, it produces the following output:
|
|
78
85
|
======The output of <code>examples/override_example_1.rb</code>:
|
79
86
|
The config:
|
80
87
|
{
|
88
|
+
addresses : [
|
89
|
+
http://default.designingpatterns.com,
|
90
|
+
http://apple.designingpatterns.com
|
91
|
+
]
|
92
|
+
contains_sensitive_data: false
|
81
93
|
os : {
|
82
94
|
version: 5.4
|
83
95
|
name : AIX
|
84
96
|
}
|
85
97
|
behind_firewall : false
|
86
98
|
num_cpus : 64
|
87
|
-
addresses : [
|
88
|
-
http://default.designingpatterns.com,
|
89
|
-
http://apple.designingpatterns.com
|
90
|
-
]
|
91
|
-
contains_sensitive_data: false
|
92
99
|
}
|
93
100
|
|
94
101
|
|
@@ -98,7 +105,7 @@ list of machine configurations via the ConfigToolkit::OverrideReader.
|
|
98
105
|
|
99
106
|
The following YAML configuration specifies a list of machine
|
100
107
|
configurations (+db1+, +db2+, ..).
|
101
|
-
|
108
|
+
======<tt>examples/load_group_example.yaml</tt>:
|
102
109
|
#
|
103
110
|
# This file contains MachineConfig configurations
|
104
111
|
# (see examples/machineconfig.rb for the configuration's specification).
|
@@ -138,7 +145,7 @@ This program specifies a MachineConfig via HashReader and adds it to
|
|
138
145
|
the list of MachineConfig configurations specified in the YAML file
|
139
146
|
above.
|
140
147
|
|
141
|
-
|
148
|
+
======<tt>examples/override_example_2.rb</tt>:
|
142
149
|
#!/usr/bin/env ruby
|
143
150
|
|
144
151
|
#
|
@@ -179,71 +186,71 @@ above.
|
|
179
186
|
# The group of configurations is under the db_cluster containing
|
180
187
|
# object name.
|
181
188
|
#
|
182
|
-
|
183
|
-
|
189
|
+
config_file_reader = ConfigToolkit::YAMLReader.new(CONFIGURATION_FILE)
|
190
|
+
hash_reader = ConfigToolkit::HashReader.new(hash_configuration)
|
191
|
+
reader = ConfigToolkit::OverrideReader.new(config_file_reader, hash_reader)
|
184
192
|
|
185
193
|
configs = MachineConfig.load_group(reader, "db_cluster")
|
186
194
|
configs.each do |name, config|
|
187
195
|
print "The #{name} configuration:\n#{config}\n"
|
188
196
|
end
|
189
|
-
|
190
197
|
|
191
198
|
When run, it produces the following output:
|
192
199
|
======The output of <code>examples/override_example_2.rb</code>:
|
193
|
-
The
|
194
|
-
db_cluster.
|
195
|
-
|
200
|
+
The db2 configuration:
|
201
|
+
db_cluster.db2: {
|
202
|
+
os : {
|
203
|
+
version: 5.3
|
204
|
+
name : AIX
|
205
|
+
}
|
206
|
+
behind_firewall : true
|
207
|
+
num_cpus : 12
|
196
208
|
addresses : [
|
197
|
-
http://
|
209
|
+
http://db2.designingpatterns.com
|
198
210
|
]
|
199
211
|
contains_sensitive_data: true
|
212
|
+
}
|
213
|
+
|
214
|
+
The db3 configuration:
|
215
|
+
db_cluster.db3: {
|
200
216
|
os : {
|
201
217
|
version: 10.0
|
202
218
|
name : Solaris
|
203
219
|
}
|
204
220
|
behind_firewall : true
|
221
|
+
num_cpus : 24
|
222
|
+
addresses : [
|
223
|
+
http://db3.designingpatterns.com
|
224
|
+
]
|
225
|
+
contains_sensitive_data: true
|
205
226
|
}
|
206
227
|
|
207
228
|
The db4 configuration:
|
208
229
|
db_cluster.db4: {
|
209
|
-
num_cpus : 48
|
210
|
-
addresses : [
|
211
|
-
http://db4.designingpatterns.com
|
212
|
-
]
|
213
|
-
contains_sensitive_data: true
|
214
230
|
os : {
|
215
231
|
version: 10.0
|
216
232
|
name : Solaris
|
217
233
|
}
|
218
234
|
behind_firewall : true
|
235
|
+
num_cpus : 48
|
236
|
+
addresses : [
|
237
|
+
http://db4.designingpatterns.com
|
238
|
+
]
|
239
|
+
contains_sensitive_data: true
|
219
240
|
}
|
220
241
|
|
221
242
|
The db1 configuration:
|
222
243
|
db_cluster.db1: {
|
223
|
-
num_cpus : 16
|
224
|
-
addresses : [
|
225
|
-
http://db1.designingpatterns.com
|
226
|
-
]
|
227
|
-
contains_sensitive_data: true
|
228
244
|
os : {
|
229
245
|
version: 10.0
|
230
246
|
name : Solaris
|
231
247
|
}
|
232
248
|
behind_firewall : true
|
233
|
-
|
234
|
-
|
235
|
-
The db2 configuration:
|
236
|
-
db_cluster.db2: {
|
237
|
-
num_cpus : 12
|
249
|
+
num_cpus : 16
|
238
250
|
addresses : [
|
239
|
-
http://
|
251
|
+
http://db1.designingpatterns.com
|
240
252
|
]
|
241
253
|
contains_sensitive_data: true
|
242
|
-
os : {
|
243
|
-
version: 5.3
|
244
|
-
name : AIX
|
245
|
-
}
|
246
|
-
behind_firewall : true
|
247
254
|
}
|
248
255
|
|
249
256
|
|
data/README.txt
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
* Project Page: http://rubyforge.org/projects/configtoolkit/
|
3
|
-
* Documentation: http://configtoolkit.rubyforge.org/
|
1
|
+
Object
|
4
2
|
|
5
3
|
== DESCRIPTION:
|
6
4
|
This package makes sourcing information from (parsing) configuration files
|
@@ -39,6 +37,16 @@ robust and easy! It:
|
|
39
37
|
optional parameters.
|
40
38
|
* A block can be passed to the +new+ method of a configuration class in order
|
41
39
|
to initialize the instance (see ConfigToolkit::BaseConfig.new).
|
40
|
+
* The ConfigToolkit (by default) will print warnings when it encounters
|
41
|
+
unknown configuration parameters, allowing errors in configuration files to
|
42
|
+
be discovered quickly.
|
43
|
+
* The behavior of the ConfigToolkit for a particular ConfigToolkit::BaseConfig
|
44
|
+
child class or for an instance of a ConfigToolkit::BaseConfig child class
|
45
|
+
can be configured through specifying a ConfigToolkit::ConfigToolkitConfig.
|
46
|
+
Right now, the ConfigToolkit's behavior when it encounters an unknown
|
47
|
+
parameter can be configured (to ignore the unknown parameter, to warn
|
48
|
+
about the unknown parameter, or to raise an error about the unknown
|
49
|
+
parameter).
|
42
50
|
* +rdoc+ can detect and generate documentation for each parameter in a
|
43
51
|
configuration class (see FAQ.txt for how to enable this).
|
44
52
|
* An equality operator exists for each configuration class that
|
@@ -99,7 +107,7 @@ None (known).
|
|
99
107
|
|
100
108
|
== SYNOPSIS:
|
101
109
|
Here is a sample configuration class:
|
102
|
-
|
110
|
+
======<tt>examples/machineconfig.rb</tt>:
|
103
111
|
require 'rubygems'
|
104
112
|
require 'configtoolkit'
|
105
113
|
|
@@ -219,7 +227,7 @@ that the Array has at least two elements. Additional constraints on the
|
|
219
227
|
number of elements in the Array are possible
|
220
228
|
|
221
229
|
The following program manipulates a MachineConfig instance.
|
222
|
-
|
230
|
+
======<tt>examples/usage_example.rb</tt>:
|
223
231
|
#!/usr/bin/env ruby
|
224
232
|
|
225
233
|
#
|
@@ -380,7 +388,7 @@ When run, it produces:
|
|
380
388
|
|
381
389
|
Of course, the most common use case will be setting configuration parameters
|
382
390
|
with the contents of configuration files, not programatically. The following YAML configuration file contains two MachineConfig configurations:
|
383
|
-
|
391
|
+
======<tt>examples/load_example.yaml</tt>:
|
384
392
|
#
|
385
393
|
# This file contains MachineConfig configurations
|
386
394
|
# (see examples/machineconfig.rb for the configuration's specification).
|
@@ -418,7 +426,7 @@ with the contents of configuration files, not programatically. The following
|
|
418
426
|
|
419
427
|
The following program loads and prints the two configurations from
|
420
428
|
+examples/load_example.yaml+.
|
421
|
-
|
429
|
+
======<tt>examples/load_example.rb</tt>:
|
422
430
|
#!/usr/bin/env ruby
|
423
431
|
|
424
432
|
#
|
@@ -493,7 +501,7 @@ configuration being accessible separately. When run, it produces:
|
|
493
501
|
A group of configurations also can be loaded with the
|
494
502
|
ConfigToolkit::BaseConfig.load_group method. For instance, given
|
495
503
|
+examples/load_group_example.yaml+:
|
496
|
-
|
504
|
+
======<tt>examples/load_group_example.yaml</tt>:
|
497
505
|
#
|
498
506
|
# This file contains MachineConfig configurations
|
499
507
|
# (see examples/machineconfig.rb for the configuration's specification).
|
@@ -530,7 +538,7 @@ ConfigToolkit::BaseConfig.load_group method. For instance, given
|
|
530
538
|
|
531
539
|
the following code will load the file's three configurations into a +Hash+
|
532
540
|
mapping containing object names to configurations:
|
533
|
-
|
541
|
+
======<tt>examples/load_group_example.rb</tt>:
|
534
542
|
#!/usr/bin/env ruby
|
535
543
|
|
536
544
|
#
|
@@ -606,6 +614,109 @@ When run, it produces:
|
|
606
614
|
|
607
615
|
|
608
616
|
|
617
|
+
== CONFIGURING THE CONFIGTOOLKIT
|
618
|
+
As of version 2.3.0, the ConfigToolkit *itself* can be configured.
|
619
|
+
|
620
|
+
Consider the following simple YAML configuration file:
|
621
|
+
======<tt>examples/config_toolkit_config_example.yaml</tt>:
|
622
|
+
name: Tony
|
623
|
+
|
624
|
+
#
|
625
|
+
# 'country' has been misspelled
|
626
|
+
#
|
627
|
+
county: USA
|
628
|
+
|
629
|
+
|
630
|
+
The following code configures the +SimpleConfig+ class and specific instances
|
631
|
+
of the +SimpleConfig+ class to behave differently than the ConfigToolkit
|
632
|
+
default when unknown parameters are encountered when loading a configuration
|
633
|
+
file.
|
634
|
+
======<tt>examples/config_toolkit_config_example.rb</tt>:
|
635
|
+
#!/usr/bin/env ruby
|
636
|
+
|
637
|
+
require 'rubygems'
|
638
|
+
require 'configtoolkit'
|
639
|
+
require 'configtoolkit/yamlreader'
|
640
|
+
|
641
|
+
require 'relative'
|
642
|
+
|
643
|
+
class SimpleConfig < ConfigToolkit::BaseConfig
|
644
|
+
|
645
|
+
#
|
646
|
+
# Set SimpleConfig's default ConfigToolkit::ConfigToolkitConfig via
|
647
|
+
# the default_config_toolkit_config accessor method.
|
648
|
+
#
|
649
|
+
default_config_toolkit_config = ConfigToolkit::ConfigToolkitConfig.new() do |instance|
|
650
|
+
#
|
651
|
+
# This will suppress warnings when an unknown config param
|
652
|
+
# is encountered when loading a configuration file.
|
653
|
+
#
|
654
|
+
instance.unknown_config_param_behavior =
|
655
|
+
ConfigToolkit::ConfigToolkitConfig::UnknownConfigParamBehavior::IGNORE
|
656
|
+
end
|
657
|
+
|
658
|
+
add_required_param(:name, String)
|
659
|
+
|
660
|
+
add_optional_param(:country, String, NO_DEFAULT_VALUE)
|
661
|
+
|
662
|
+
end
|
663
|
+
|
664
|
+
CONFIGURATION_FILE = File.expand_path_relative_to_caller("config_toolkit_config_example.yaml")
|
665
|
+
|
666
|
+
#
|
667
|
+
# Even though there is a bogus parameter in the configuration file,
|
668
|
+
# SimpleConfig.load silently will ignore it due to SimpleConfig's
|
669
|
+
# default ConfigToolkit::ConfigToolkitConfig.
|
670
|
+
#
|
671
|
+
reader = ConfigToolkit::YAMLReader.new(CONFIGURATION_FILE)
|
672
|
+
config = SimpleConfig.load(reader)
|
673
|
+
puts "Unknown parameters silently were ignored when loading this: #{config}\n"
|
674
|
+
|
675
|
+
#
|
676
|
+
# Note that a ConfigToolkit::ConfigToolkitConfig is passed
|
677
|
+
# into SimpleConfig.load below. This will override SimpleConfig's default
|
678
|
+
# ConfigToolkit::ConfigToolkitConfig and cause an error to be raised when
|
679
|
+
# the unknown parameter is encountered.
|
680
|
+
#
|
681
|
+
config_toolkit_config = ConfigToolkit::ConfigToolkitConfig.new() do |instance|
|
682
|
+
#
|
683
|
+
# This will cause an error to be raised when an unknown config param
|
684
|
+
# is encountered when loading a configuration file.
|
685
|
+
#
|
686
|
+
instance.unknown_config_param_behavior =
|
687
|
+
ConfigToolkit::ConfigToolkitConfig::UnknownConfigParamBehavior::ERROR
|
688
|
+
end
|
689
|
+
reader = ConfigToolkit::YAMLReader.new(CONFIGURATION_FILE)
|
690
|
+
|
691
|
+
begin
|
692
|
+
config = SimpleConfig.load(reader, "", config_toolkit_config)
|
693
|
+
rescue ConfigToolkit::Error => e
|
694
|
+
puts e
|
695
|
+
end
|
696
|
+
|
697
|
+
|
698
|
+
When run, it produces:
|
699
|
+
======The output of <code>examples/config_toolkit_config_example.rb</code>:
|
700
|
+
Unknown parameters silently were ignored when loading this: {
|
701
|
+
name: Tony
|
702
|
+
}
|
703
|
+
|
704
|
+
error setting county with value USA: no parameter county exists for configuration class SimpleConfig.
|
705
|
+
|
706
|
+
|
707
|
+
|
708
|
+
=== CONFIGTOOLKIT CONFIGURATION PARAMETERS
|
709
|
+
+unknown_config_param_behavior+: the behavior when encountering an unknown parameter. Its possible values:
|
710
|
+
+ignore+:: The unknown parameter will be ignored (+ignore+). This can be
|
711
|
+
useful in order to maintain compatibility with older versions of
|
712
|
+
software when adding new fields to a configuration file.
|
713
|
+
+warn+:: The unknown parameter will cause a warning to be issued (+warn+).
|
714
|
+
This is the default and can allow errors in configuration files to
|
715
|
+
be detected more easily. It does *NOT* prevent the configuration
|
716
|
+
file from being loaded, however.
|
717
|
+
+error+:: The unknown parameter will cause an error to be raised, preventing
|
718
|
+
the configuration file from being loaded.
|
719
|
+
|
609
720
|
== CONFIGURATION FILE FORMATS:
|
610
721
|
=== EXISTING FORMATS:
|
611
722
|
* YAML.txt describes working with YAML configuration files.
|
@@ -707,12 +818,12 @@ desirable!
|
|
707
818
|
* Homepage: http://www.designingpatterns.com
|
708
819
|
* Blogs: http://blogs.designingpatterns.com
|
709
820
|
|
710
|
-
== SUPPORT
|
821
|
+
== SUPPORT:
|
711
822
|
Please post questions, concerns, or requests for enhancement to the forums on
|
712
823
|
the project page. Alternatively, direct contact information for
|
713
824
|
Designing Patterns can be found on the project page for this gem.
|
714
825
|
|
715
|
-
== ENHANCEMENTS
|
826
|
+
== ENHANCEMENTS:
|
716
827
|
Please feel free to contact us with any ideas; we will try our best to
|
717
828
|
enhance the software and respond to user requests. Of course, we are more
|
718
829
|
likely to work on a particular enhancement if we know that there are users
|
data/Rakefile
CHANGED
data/Ruby.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
|
2
|
+
|
1
3
|
== RUBY CONFIGURATION FILES:
|
2
4
|
What could be better than configuring Ruby programs with Ruby
|
3
5
|
configuration files, harnessing the full power of Ruby for
|
@@ -28,7 +30,7 @@ static expressions.
|
|
28
30
|
|
29
31
|
== EXAMPLE:
|
30
32
|
This is a Ruby configuration file:
|
31
|
-
|
33
|
+
======<tt>examples/ruby_example.rcfg</tt>:
|
32
34
|
#
|
33
35
|
# This file contains MachineConfig configurations
|
34
36
|
# (see examples/machineconfig.rb for the configuration's specification).
|
@@ -83,7 +85,7 @@ This is a Ruby configuration file:
|
|
83
85
|
]
|
84
86
|
|
85
87
|
This program loads the two configurations in the file:
|
86
|
-
|
88
|
+
======<tt>examples/ruby_example.rb</tt>:
|
87
89
|
#!/usr/bin/env ruby
|
88
90
|
|
89
91
|
#
|
data/YAML.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
|
2
|
+
|
1
3
|
== YAML CONFIGURATION FILES:
|
2
4
|
YAML is a clean, flexible data representation language that easily can
|
3
5
|
represent complicated configurations. It natively supports simple
|
@@ -15,7 +17,7 @@ The parameter values in YAML configuration files can be native YAML
|
|
15
17
|
types (strings, integers, floats, booleans, etc.) and also can be
|
16
18
|
serialized Ruby classes. The following configuration file
|
17
19
|
demonstrates this:
|
18
|
-
|
20
|
+
======<tt>examples/yaml_example.yaml</tt>:
|
19
21
|
#
|
20
22
|
# This file contains MachineConfig configurations
|
21
23
|
# (see examples/machineconfig.rb for the configuration's specification).
|
@@ -84,7 +86,7 @@ the file; FAQ.txt discusses how the ConfigToolkit converts YAML
|
|
84
86
|
strings to some Ruby types (Pathnames, URIs, Symbols, etc.). The following
|
85
87
|
program shows how this example YAML configuration file can be loaded,
|
86
88
|
and how a configuration can be written as a YAML file:
|
87
|
-
|
89
|
+
======<tt>examples/yaml_example.rb</tt>:
|
88
90
|
#!/usr/bin/env ruby
|
89
91
|
|
90
92
|
#
|
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'configtoolkit'
|
5
|
+
require 'configtoolkit/yamlreader'
|
6
|
+
|
7
|
+
require 'relative'
|
8
|
+
|
9
|
+
class SimpleConfig < ConfigToolkit::BaseConfig
|
10
|
+
|
11
|
+
#
|
12
|
+
# Set SimpleConfig's default ConfigToolkit::ConfigToolkitConfig via
|
13
|
+
# the default_config_toolkit_config accessor method.
|
14
|
+
#
|
15
|
+
default_config_toolkit_config = ConfigToolkit::ConfigToolkitConfig.new() do |instance|
|
16
|
+
#
|
17
|
+
# This will suppress warnings when an unknown config param
|
18
|
+
# is encountered when loading a configuration file.
|
19
|
+
#
|
20
|
+
instance.unknown_config_param_behavior =
|
21
|
+
ConfigToolkit::ConfigToolkitConfig::UnknownConfigParamBehavior::IGNORE
|
22
|
+
end
|
23
|
+
|
24
|
+
add_required_param(:name, String)
|
25
|
+
|
26
|
+
add_optional_param(:country, String, NO_DEFAULT_VALUE)
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
CONFIGURATION_FILE = File.expand_path_relative_to_caller("config_toolkit_config_example.yaml")
|
31
|
+
|
32
|
+
#
|
33
|
+
# Even though there is a bogus parameter in the configuration file,
|
34
|
+
# SimpleConfig.load silently will ignore it due to SimpleConfig's
|
35
|
+
# default ConfigToolkit::ConfigToolkitConfig.
|
36
|
+
#
|
37
|
+
reader = ConfigToolkit::YAMLReader.new(CONFIGURATION_FILE)
|
38
|
+
config = SimpleConfig.load(reader)
|
39
|
+
puts "Unknown parameters silently were ignored when loading this: #{config}\n"
|
40
|
+
|
41
|
+
#
|
42
|
+
# Note that a ConfigToolkit::ConfigToolkitConfig is passed
|
43
|
+
# into SimpleConfig.load below. This will override SimpleConfig's default
|
44
|
+
# ConfigToolkit::ConfigToolkitConfig and cause an error to be raised when
|
45
|
+
# the unknown parameter is encountered.
|
46
|
+
#
|
47
|
+
config_toolkit_config = ConfigToolkit::ConfigToolkitConfig.new() do |instance|
|
48
|
+
#
|
49
|
+
# This will cause an error to be raised when an unknown config param
|
50
|
+
# is encountered when loading a configuration file.
|
51
|
+
#
|
52
|
+
instance.unknown_config_param_behavior =
|
53
|
+
ConfigToolkit::ConfigToolkitConfig::UnknownConfigParamBehavior::ERROR
|
54
|
+
end
|
55
|
+
reader = ConfigToolkit::YAMLReader.new(CONFIGURATION_FILE)
|
56
|
+
|
57
|
+
begin
|
58
|
+
config = SimpleConfig.load(reader, "", config_toolkit_config)
|
59
|
+
rescue ConfigToolkit::Error => e
|
60
|
+
puts e
|
61
|
+
end
|
@@ -16,11 +16,16 @@ require_relative 'machineconfig'
|
|
16
16
|
require 'configtoolkit/yamlreader'
|
17
17
|
require 'configtoolkit/overridereader'
|
18
18
|
|
19
|
-
CONFIGURATION_FILE =
|
20
|
-
|
19
|
+
CONFIGURATION_FILE =
|
20
|
+
File.expand_path_relative_to_caller("override_example.yaml")
|
21
|
+
OVERRIDE_FILE =
|
22
|
+
File.expand_path_relative_to_caller("override_example_override.yaml")
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
+
config_file_reader = ConfigToolkit::YAMLReader.new(CONFIGURATION_FILE)
|
25
|
+
override_file_reader = ConfigToolkit::YAMLReader.new(OVERRIDE_FILE)
|
26
|
+
|
27
|
+
reader = ConfigToolkit::OverrideReader.new(config_file_reader,
|
28
|
+
override_file_reader)
|
24
29
|
|
25
30
|
# Let's load the overriden configuration.
|
26
31
|
config = MachineConfig.load(reader)
|
@@ -38,11 +38,11 @@ hash_configuration = {
|
|
38
38
|
# The group of configurations is under the db_cluster containing
|
39
39
|
# object name.
|
40
40
|
#
|
41
|
-
|
42
|
-
|
41
|
+
config_file_reader = ConfigToolkit::YAMLReader.new(CONFIGURATION_FILE)
|
42
|
+
hash_reader = ConfigToolkit::HashReader.new(hash_configuration)
|
43
|
+
reader = ConfigToolkit::OverrideReader.new(config_file_reader, hash_reader)
|
43
44
|
|
44
45
|
configs = MachineConfig.load_group(reader, "db_cluster")
|
45
46
|
configs.each do |name, config|
|
46
47
|
print "The #{name} configuration:\n#{config}\n"
|
47
48
|
end
|
48
|
-
|
@@ -163,6 +163,7 @@ class BaseConfig
|
|
163
163
|
child_class.class_eval do
|
164
164
|
@param_spec_list = []
|
165
165
|
@param_spec_lookup_table = {}
|
166
|
+
@default_config_toolkit_config = nil
|
166
167
|
end
|
167
168
|
end
|
168
169
|
|
@@ -424,6 +425,52 @@ class BaseConfig
|
|
424
425
|
end
|
425
426
|
private_class_method :add_optional_param
|
426
427
|
|
428
|
+
#
|
429
|
+
# ====Description:
|
430
|
+
# This method returns the default ConfigToolkitConfig instance for
|
431
|
+
# this BaseConfig child class (each BaseConfig child class can have a
|
432
|
+
# different default ConfigToolkitConfig instance). The loading/dumping
|
433
|
+
# processes of all instances of the BaseConfig child class will be
|
434
|
+
# configured by this ConfigToolkitConfig instance unless a different
|
435
|
+
# instance is passed into BaseConfig.load or BaseConfig#initialize.
|
436
|
+
#
|
437
|
+
# ====Returns:
|
438
|
+
# The default ConfigToolkitConfig instance for this BaseConfig child class.
|
439
|
+
#
|
440
|
+
def self.default_config_toolkit_config
|
441
|
+
if(@default_config_toolkit_config) then
|
442
|
+
return @default_config_toolkit_config
|
443
|
+
else
|
444
|
+
#
|
445
|
+
# We need to check whether ConfigToolkitConfig::DEFAULT_CONFIG is
|
446
|
+
# defined because it won't be when the ConfigToolkit is bootstrapping
|
447
|
+
# itself (since the ConfigToolkitConfig is a BaseConfig child class).
|
448
|
+
#
|
449
|
+
if(ConfigToolkit.const_defined?(:ConfigToolkitConfig) && ConfigToolkitConfig.const_defined?(:DEFAULT_CONFIG)) then
|
450
|
+
return ConfigToolkitConfig::DEFAULT_CONFIG
|
451
|
+
else
|
452
|
+
return nil
|
453
|
+
end
|
454
|
+
end
|
455
|
+
end
|
456
|
+
private_class_method :default_config_toolkit_config
|
457
|
+
|
458
|
+
#
|
459
|
+
# ====Description:
|
460
|
+
# This method sets the default ConfigToolkitConfig instance for this
|
461
|
+
# BaseConfig child class to config_toolkit_config (each BaseConfig child
|
462
|
+
# class can have a different default config_toolkit_config).
|
463
|
+
#
|
464
|
+
# ====Parameters:
|
465
|
+
# [config_toolkit_config]
|
466
|
+
# The new default ConfigToolkitConfig instance for this BaseConfig
|
467
|
+
# child class.
|
468
|
+
#
|
469
|
+
def self.default_config_toolkit_config=(config_toolkit_config)
|
470
|
+
@default_config_toolkit_config = config_toolkit_config
|
471
|
+
end
|
472
|
+
private_class_method :default_config_toolkit_config=
|
473
|
+
|
427
474
|
#
|
428
475
|
# This is a String containing the name of the object that
|
429
476
|
# contains this instance's configuration. For example, if
|
@@ -460,12 +507,20 @@ class BaseConfig
|
|
460
507
|
# end
|
461
508
|
#
|
462
509
|
# ====Parameters:
|
510
|
+
# [config_toolkit_config]
|
511
|
+
# An optional ConfigToolkitConfig instance to configure the new
|
512
|
+
# configuration instance (this allows the loading/dumping process to
|
513
|
+
# be customized). If this parameter is not specified by the caller,
|
514
|
+
# then the value of BaseConfig.default_config_toolkit_config will be
|
515
|
+
# used.
|
463
516
|
# [&initialization_block]
|
464
517
|
# A block that fully initializes the parameters of the new
|
465
518
|
# instance
|
466
519
|
#
|
467
|
-
def initialize(
|
520
|
+
def initialize(config_toolkit_config = nil,
|
521
|
+
&initialization_block)
|
468
522
|
@containing_object_name = ""
|
523
|
+
@config_toolkit_config = config_toolkit_config
|
469
524
|
|
470
525
|
clear_all_values()
|
471
526
|
|
@@ -475,6 +530,26 @@ class BaseConfig
|
|
475
530
|
end
|
476
531
|
end
|
477
532
|
|
533
|
+
#
|
534
|
+
# ====Description:
|
535
|
+
# This method returns the config_toolkit_config for this instance (used to
|
536
|
+
# configure the loading/dumping process).
|
537
|
+
#
|
538
|
+
# ====Returns:
|
539
|
+
# the config_toolkit_config for this instance
|
540
|
+
#
|
541
|
+
def config_toolkit_config
|
542
|
+
if(@config_toolkit_config) then
|
543
|
+
return @config_toolkit_config
|
544
|
+
else
|
545
|
+
#
|
546
|
+
# Use send to call the private default_config_toolkit_config method.
|
547
|
+
#
|
548
|
+
return self.class.send(:default_config_toolkit_config)
|
549
|
+
end
|
550
|
+
end
|
551
|
+
private :config_toolkit_config
|
552
|
+
|
478
553
|
#
|
479
554
|
# ====Description:
|
480
555
|
# This method clears all parameter values.
|
@@ -963,13 +1038,17 @@ class BaseConfig
|
|
963
1038
|
#
|
964
1039
|
clear_all_values()
|
965
1040
|
|
1041
|
+
config_toolkit_config_value = config_toolkit_config
|
1042
|
+
unknown_config_param_behavior = config_toolkit_config_value.unknown_config_param_behavior
|
1043
|
+
|
966
1044
|
if(containing_object_hash != nil)
|
967
1045
|
containing_object_hash.each do |name, value|
|
968
1046
|
#
|
969
1047
|
# Lookup the parameter specification by name. If the parameter is
|
970
|
-
# not found, the config file must have extra parameters in it
|
971
|
-
#
|
972
|
-
#
|
1048
|
+
# not found, the config file must have extra parameters in it;
|
1049
|
+
# handling of such unknown parameters is configured by the
|
1050
|
+
# instance's ConfigToolkitConfig (the default is to warn about the
|
1051
|
+
# extra parameters).
|
973
1052
|
#
|
974
1053
|
# The parameter name in the containing object hash either could be a
|
975
1054
|
# Symbol or a String; in either case to_sym() will normalize
|
@@ -979,6 +1058,20 @@ class BaseConfig
|
|
979
1058
|
param_spec = self.class.param_spec_lookup_table[name.to_sym()]
|
980
1059
|
|
981
1060
|
if(param_spec == nil)
|
1061
|
+
if(unknown_config_param_behavior != ConfigToolkitConfig::UnknownConfigParamBehavior::IGNORE) then
|
1062
|
+
error_message = self.class.send(:construct_error_message,
|
1063
|
+
containing_object_name,
|
1064
|
+
name,
|
1065
|
+
value,
|
1066
|
+
"no parameter #{name} exists for configuration class #{self.class}")
|
1067
|
+
|
1068
|
+
if(unknown_config_param_behavior == ConfigToolkitConfig::UnknownConfigParamBehavior::WARN) then
|
1069
|
+
$stderr.puts("WARNING: #{error_message} Ignoring unknown parameter #{name}!")
|
1070
|
+
else
|
1071
|
+
raise Error, error_message
|
1072
|
+
end
|
1073
|
+
end
|
1074
|
+
|
982
1075
|
next
|
983
1076
|
end
|
984
1077
|
|
@@ -1026,12 +1119,18 @@ class BaseConfig
|
|
1026
1119
|
#
|
1027
1120
|
# ====Parameters:
|
1028
1121
|
# See the parameter list for load.
|
1122
|
+
# [config_toolkit_config]
|
1123
|
+
# An optional ConfigToolkitConfig instance to configure the new
|
1124
|
+
# configuration instance (this allows the loading/dumping process to
|
1125
|
+
# be customized). If this parameter is not specified by the caller,
|
1126
|
+
# then the value of BaseConfig.default_config_toolkit_config will be
|
1127
|
+
# used.
|
1029
1128
|
#
|
1030
1129
|
# ====Returns:
|
1031
1130
|
# The new instance, preloaded!
|
1032
1131
|
#
|
1033
|
-
def self.load(reader, containing_object_name = "")
|
1034
|
-
instance = new()
|
1132
|
+
def self.load(reader, containing_object_name = "", config_toolkit_config = nil)
|
1133
|
+
instance = new(config_toolkit_config)
|
1035
1134
|
instance.load(reader, containing_object_name)
|
1036
1135
|
return instance
|
1037
1136
|
end
|
@@ -1046,11 +1145,17 @@ class BaseConfig
|
|
1046
1145
|
#
|
1047
1146
|
# ====Parameters:
|
1048
1147
|
# See the parameter list for load.
|
1148
|
+
# [config_toolkit_config]
|
1149
|
+
# An optional ConfigToolkitConfig instance to configure the new
|
1150
|
+
# configuration instance (this allows the loading/dumping process to
|
1151
|
+
# be customized). If this parameter is not specified by the caller,
|
1152
|
+
# then the value of BaseConfig.default_config_toolkit_config will be
|
1153
|
+
# used.
|
1049
1154
|
#
|
1050
1155
|
# ====Returns:
|
1051
1156
|
# A Hash of names (Symbols) mapping to loaded configuration instances
|
1052
1157
|
#
|
1053
|
-
def self.load_group(reader, containing_object_name = "")
|
1158
|
+
def self.load_group(reader, containing_object_name = "", config_toolkit_config = nil)
|
1054
1159
|
containing_object_hash = load_containing_object_hash(reader, containing_object_name)
|
1055
1160
|
|
1056
1161
|
config_group = {}
|
@@ -1066,7 +1171,7 @@ class BaseConfig
|
|
1066
1171
|
raise Error, message
|
1067
1172
|
end
|
1068
1173
|
|
1069
|
-
instance = new()
|
1174
|
+
instance = new(config_toolkit_config)
|
1070
1175
|
|
1071
1176
|
#
|
1072
1177
|
# Have to use send in order to call private method.
|
@@ -1109,3 +1214,9 @@ class BaseConfig
|
|
1109
1214
|
end
|
1110
1215
|
|
1111
1216
|
end
|
1217
|
+
|
1218
|
+
#
|
1219
|
+
# This must be loaded after BaseConfig has been defined because it
|
1220
|
+
# defines a child of BaseConfig.
|
1221
|
+
#
|
1222
|
+
require 'configtoolkit/configtoolkitconfig'
|
@@ -0,0 +1,77 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'configtoolkit/baseconfig'
|
4
|
+
|
5
|
+
module ConfigToolkit
|
6
|
+
|
7
|
+
#
|
8
|
+
# This class configures the ConfigToolkit itself (or, more precisely,
|
9
|
+
# configures instances of BaseConfig child classes). Right now, it
|
10
|
+
# allows customization of the behavior of a configuration instance when
|
11
|
+
# it encounters an unknown configuration parameter during
|
12
|
+
# configuration loading.
|
13
|
+
#
|
14
|
+
class ConfigToolkitConfig < BaseConfig
|
15
|
+
|
16
|
+
#
|
17
|
+
# This module is a (hacky) enumeration of
|
18
|
+
# the possible values for the ConfigToolkitConfig's
|
19
|
+
# +unknown_config_param_behavior+ parameter.
|
20
|
+
#
|
21
|
+
module UnknownConfigParamBehavior
|
22
|
+
#
|
23
|
+
# The ConfigToolkit will ignore unknown configuration parameters
|
24
|
+
# when loading a configuration.
|
25
|
+
#
|
26
|
+
IGNORE = :ignore
|
27
|
+
|
28
|
+
#
|
29
|
+
# The ConfigToolkit will warn about unknown configuration parameters
|
30
|
+
# when loading a configuration. This is the default.
|
31
|
+
#
|
32
|
+
WARN = :warn
|
33
|
+
|
34
|
+
#
|
35
|
+
# The ConfigToolkit will raise an error when it encounters an unknown
|
36
|
+
# configuration paramater when loading a configuration.
|
37
|
+
#
|
38
|
+
ERROR = :error
|
39
|
+
|
40
|
+
#
|
41
|
+
# ====Description:
|
42
|
+
# This class method returns +true+ if and only if
|
43
|
+
# +value+ is a valid UnknownConfigParamBehavior
|
44
|
+
# constant.
|
45
|
+
#
|
46
|
+
# ====Parameters:
|
47
|
+
# [value]
|
48
|
+
# The value to verify
|
49
|
+
#
|
50
|
+
# ====Returns:
|
51
|
+
# +True+ if and only if +value+ is a valid UnknownConfigParamBehavior
|
52
|
+
# constant.
|
53
|
+
#
|
54
|
+
def self.valid?(value)
|
55
|
+
return (value == UnknownConfigParamBehavior::IGNORE ||
|
56
|
+
value == UnknownConfigParamBehavior::WARN ||
|
57
|
+
value == UnknownConfigParamBehavior::ERROR)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
#
|
62
|
+
# How the ConfigToolkit should behave when it encounters
|
63
|
+
# an unknown configuration parameter.
|
64
|
+
#
|
65
|
+
add_optional_param(:unknown_config_param_behavior,
|
66
|
+
Symbol,
|
67
|
+
UnknownConfigParamBehavior::WARN) do |new_value|
|
68
|
+
if(!UnknownConfigParamBehavior.valid?(new_value)) then
|
69
|
+
raise_error("#{new_value} is not a valid unknown_config_param_behavior value")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
DEFAULT_CONFIG = self.new(){}
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
data/test/firewall.yaml
CHANGED
data/test/test_baseconfig.rb
CHANGED
@@ -52,6 +52,16 @@ class BaseConfigTest < Test::Unit::TestCase
|
|
52
52
|
# 3.) A ConstrainedArray parameter
|
53
53
|
#
|
54
54
|
class MachineConfig < ConfigToolkit::BaseConfig
|
55
|
+
|
56
|
+
#
|
57
|
+
# This will quiet lots of warnings, as the configuration files
|
58
|
+
# for the tests have lots of bogus parameters.
|
59
|
+
#
|
60
|
+
CONFIG_TOOLKIT_CONFIG = ConfigToolkit::ConfigToolkitConfig.new() do |instance|
|
61
|
+
instance.unknown_config_param_behavior = ConfigToolkit::ConfigToolkitConfig::UnknownConfigParamBehavior::IGNORE
|
62
|
+
end
|
63
|
+
default_config_toolkit_config = CONFIG_TOOLKIT_CONFIG
|
64
|
+
|
55
65
|
add_required_param(:name, String)
|
56
66
|
|
57
67
|
add_required_param(:architecture, String)
|
@@ -434,4 +444,91 @@ class BaseConfigTest < Test::Unit::TestCase
|
|
434
444
|
visitor.visit(3)
|
435
445
|
end
|
436
446
|
end
|
447
|
+
|
448
|
+
def test_config_toolkit_config
|
449
|
+
|
450
|
+
begin
|
451
|
+
#
|
452
|
+
# This config_toolkit_config will:
|
453
|
+
# * Warn when unknown config params are encountered.
|
454
|
+
#
|
455
|
+
config_toolkit_config1 = ConfigToolkit::ConfigToolkitConfig.new() do |instance|
|
456
|
+
instance.unknown_config_param_behavior = ConfigToolkit::ConfigToolkitConfig::UnknownConfigParamBehavior::WARN
|
457
|
+
end
|
458
|
+
|
459
|
+
config_file_name = File.expand_path_relative_to_caller("firewall.yaml")
|
460
|
+
|
461
|
+
#
|
462
|
+
# Specify the config_toolkit_config for an instance;
|
463
|
+
# loading the configuration file should result in a warning (which
|
464
|
+
# we unfortunately have no easy way to verify, except visually).
|
465
|
+
#
|
466
|
+
reader = ConfigToolkit::YAMLReader.new(config_file_name)
|
467
|
+
MachineConfig.load(reader, "firewall", config_toolkit_config1)
|
468
|
+
|
469
|
+
#
|
470
|
+
# Specify the config_toolkit_config for the MachineConfig class.
|
471
|
+
#
|
472
|
+
MachineConfig.send(:default_config_toolkit_config=, config_toolkit_config1)
|
473
|
+
|
474
|
+
#
|
475
|
+
# Now we should see warnings when even not specifying the
|
476
|
+
# config_toolkit_config when constructing the instance, since we've
|
477
|
+
# specified it at the class level.
|
478
|
+
#
|
479
|
+
reader = ConfigToolkit::YAMLReader.new(config_file_name)
|
480
|
+
MachineConfig.load(reader, "firewall")
|
481
|
+
|
482
|
+
#
|
483
|
+
# This config_toolkit_config will:
|
484
|
+
# * Raise errors when unknown config params are encountered.
|
485
|
+
#
|
486
|
+
config_toolkit_config2 = ConfigToolkit::ConfigToolkitConfig.new() do |instance|
|
487
|
+
instance.unknown_config_param_behavior = ConfigToolkit::ConfigToolkitConfig::UnknownConfigParamBehavior::ERROR
|
488
|
+
end
|
489
|
+
|
490
|
+
#
|
491
|
+
# Specify the config_toolkit_config for an instance.
|
492
|
+
# This should result in an Error being raised when
|
493
|
+
# the configuration file is loaded (since it has an
|
494
|
+
# unknown parameter).
|
495
|
+
#
|
496
|
+
reader = ConfigToolkit::YAMLReader.new(config_file_name)
|
497
|
+
assert_raise_message("error setting firewall.bogus_param with value boo: no parameter bogus_param exists for configuration class BaseConfigTest::MachineConfig.",
|
498
|
+
ConfigToolkit::Error) do
|
499
|
+
MachineConfig.load(reader, "firewall", config_toolkit_config2)
|
500
|
+
end
|
501
|
+
|
502
|
+
#
|
503
|
+
# We still only should get warnings when not specifying a
|
504
|
+
# config_toolkit_config for an instance since we have not changed
|
505
|
+
# the class-level config_toolkit_config instance yet.
|
506
|
+
#
|
507
|
+
reader = ConfigToolkit::YAMLReader.new(config_file_name)
|
508
|
+
MachineConfig.load(reader, "firewall")
|
509
|
+
|
510
|
+
#
|
511
|
+
# Now specify the new config_toolkit_config for the class.
|
512
|
+
#
|
513
|
+
MachineConfig.send(:default_config_toolkit_config=, config_toolkit_config2)
|
514
|
+
|
515
|
+
#
|
516
|
+
# Now we should get an Error when not loading up a configuration file
|
517
|
+
# with an unknown parameter.
|
518
|
+
#
|
519
|
+
reader = ConfigToolkit::YAMLReader.new(config_file_name)
|
520
|
+
assert_raise_message("error setting firewall.bogus_param with value boo: no parameter bogus_param exists for configuration class BaseConfigTest::MachineConfig.",
|
521
|
+
ConfigToolkit::Error) do
|
522
|
+
MachineConfig.load(reader, "firewall")
|
523
|
+
end
|
524
|
+
|
525
|
+
ensure
|
526
|
+
#
|
527
|
+
# Make sure to restore the default config_tookit_config after
|
528
|
+
# this test.
|
529
|
+
#
|
530
|
+
MachineConfig.send(:default_config_toolkit_config=, MachineConfig::CONFIG_TOOLKIT_CONFIG)
|
531
|
+
end
|
532
|
+
|
533
|
+
end
|
437
534
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'configtoolkit'
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'assertions'
|
7
|
+
|
8
|
+
require 'test/unit'
|
9
|
+
|
10
|
+
class ConfigToolkitConfigTest < Test::Unit::TestCase
|
11
|
+
|
12
|
+
def test_new
|
13
|
+
config1 = ConfigToolkit::ConfigToolkitConfig.new() # empty configuration
|
14
|
+
|
15
|
+
config2 = ConfigToolkit::ConfigToolkitConfig.new() {} # default
|
16
|
+
|
17
|
+
config3 = ConfigToolkit::ConfigToolkitConfig.new() do |config|
|
18
|
+
config.unknown_config_param_behavior = ConfigToolkit::ConfigToolkitConfig::UnknownConfigParamBehavior::WARN
|
19
|
+
end
|
20
|
+
assert_equal(ConfigToolkit::ConfigToolkitConfig::UnknownConfigParamBehavior::WARN, config3.unknown_config_param_behavior)
|
21
|
+
|
22
|
+
assert_raise_message("error setting unknown_config_param_behavior with value boo: boo is not a valid unknown_config_param_behavior value.", ConfigToolkit::Error) do
|
23
|
+
config3.unknown_config_param_behavior = :boo
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
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: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DesigningPatterns
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-06-09 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -40,7 +40,7 @@ dependencies:
|
|
40
40
|
requirements:
|
41
41
|
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: 1.
|
43
|
+
version: 1.8.0
|
44
44
|
version:
|
45
45
|
description: "This package makes sourcing information from (parsing) configuration files robust and easy! It: * Allows programmers to specify the type of data that should be loaded from a configuration file. The toolkit automatically will validate the file's data against this specification when loading the file, ensuring that the specification always is obeyed and saving the programmer the tedious chore of writing validation code. * Automagically generates parameter accessor methods (getters, setters, and predicates to test for presence), an equality operator, and a +to_s+ method from the configuration's specification. * Allows programmers to create configuration files, easily and programatically. * Provides a class that can load (parse) Ruby configuration files (allowing the full power of Ruby to be used within configuration files). * Provides classes that can load from (parse) and dump to YAML and key-value configuration files. * Provides classes that can load from and dump to Hashes. * Provides a class that allows the contents of one configuration source to override the contents of another (this works with configuration files of any format or Hashes). * Is very extensible, allowing the engine to be used with custom format configuration files and with custom data validation rules."
|
46
46
|
email:
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- Ruby.txt
|
72
72
|
- YAML.txt
|
73
73
|
- Rakefile
|
74
|
+
- examples/config_toolkit_config_example.rb
|
74
75
|
- examples/hash_example.rb
|
75
76
|
- examples/key_value_example.rb
|
76
77
|
- examples/load_example.rb
|
@@ -81,6 +82,7 @@ files:
|
|
81
82
|
- examples/ruby_example.rb
|
82
83
|
- examples/usage_example.rb
|
83
84
|
- examples/yaml_example.rb
|
85
|
+
- examples/config_toolkit_config_example.yaml
|
84
86
|
- examples/load_example.yaml
|
85
87
|
- examples/load_group_example.yaml
|
86
88
|
- examples/override_example_override.yaml
|
@@ -94,6 +96,7 @@ files:
|
|
94
96
|
- examples/ruby_example.rcfg
|
95
97
|
- lib/configtoolkit.rb
|
96
98
|
- lib/configtoolkit/baseconfig.rb
|
99
|
+
- lib/configtoolkit/configtoolkitconfig.rb
|
97
100
|
- lib/configtoolkit/hasharrayvisitor.rb
|
98
101
|
- lib/configtoolkit/hashreader.rb
|
99
102
|
- lib/configtoolkit/hashwriter.rb
|
@@ -112,6 +115,7 @@ files:
|
|
112
115
|
- rdoc_support/rdoc-2.1.0.patch
|
113
116
|
- test/readerwritertest.rb
|
114
117
|
- test/test_baseconfig.rb
|
118
|
+
- test/test_configtoolkitconfig.rb
|
115
119
|
- test/test_hash.rb
|
116
120
|
- test/test_keyvalue.rb
|
117
121
|
- test/test_override.rb
|
@@ -157,7 +161,7 @@ files:
|
|
157
161
|
- test/contained_sample.rcfg
|
158
162
|
- test/sample.rcfg
|
159
163
|
has_rdoc: true
|
160
|
-
homepage:
|
164
|
+
homepage: This package makes sourcing information from (parsing) configuration files
|
161
165
|
post_install_message:
|
162
166
|
rdoc_options:
|
163
167
|
- --main
|
@@ -188,6 +192,7 @@ test_files:
|
|
188
192
|
- test/test_override.rb
|
189
193
|
- test/test_ruby.rb
|
190
194
|
- test/test_yaml.rb
|
195
|
+
- test/test_configtoolkitconfig.rb
|
191
196
|
- test/test_keyvalue.rb
|
192
197
|
- test/test_prettyprint.rb
|
193
198
|
- test/test_hash.rb
|