configfiles 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +3 -0
- data/lib/configfiles.rb +33 -14
- metadata +2 -2
data/Changelog
CHANGED
data/lib/configfiles.rb
CHANGED
@@ -7,7 +7,7 @@ require 'configfiles/extensions/enumerable'
|
|
7
7
|
|
8
8
|
module ConfigFiles
|
9
9
|
|
10
|
-
VERSION = '0.
|
10
|
+
VERSION = '0.3.0'
|
11
11
|
|
12
12
|
# You should write a read(io) method,
|
13
13
|
# taking an IO object and returnig a key-value hash, where keys
|
@@ -65,9 +65,8 @@ module ConfigFiles
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
|
-
# +circumstance+ must be an
|
69
|
-
#
|
70
|
-
# returns an elements of +PREDEFINED_ACTIONS+ or a user-defined Proc
|
68
|
+
# +circumstance+ must be an element of +CIRCUMSTANCES+ .
|
69
|
+
# Returns an element of +PREDEFINED_ACTIONS+ or a user-defined Proc
|
71
70
|
def behavior_on(circumstance); on(circumstance); end
|
72
71
|
|
73
72
|
# Add a parameter.
|
@@ -173,7 +172,7 @@ module ConfigFiles
|
|
173
172
|
end
|
174
173
|
end
|
175
174
|
|
176
|
-
# Set
|
175
|
+
# Set validation rules. For example, if parameter 'a' must be
|
177
176
|
# smaller than 'b':
|
178
177
|
#
|
179
178
|
# validate do |confdata|
|
@@ -200,9 +199,27 @@ module ConfigFiles
|
|
200
199
|
# Load the Hash h onto the ConfigFiles object, carrying on conversions
|
201
200
|
# to Ruby objects, validation, and default actions
|
202
201
|
# if needed. h's keys are Symbols, h's values are typically Strings
|
203
|
-
# or Enumerables yielding Strings. See also ConfigFiles::Base
|
204
|
-
# and ConfigFiles::Base
|
205
|
-
|
202
|
+
# or Enumerables yielding Strings. See also ConfigFiles::Base::parameter
|
203
|
+
# and ConfigFiles::Base::on.
|
204
|
+
#
|
205
|
+
# Option Hash opt_h keys:
|
206
|
+
#
|
207
|
+
# * +:compute_defaults+ (default +true+)
|
208
|
+
# compute/assign default values to unset params if possible
|
209
|
+
#
|
210
|
+
# * +:compute_deferred+ (default +true+)
|
211
|
+
# compute/assign parameters which are function of others
|
212
|
+
#
|
213
|
+
# * +:validate+ (default +true+)
|
214
|
+
# perform validation defined in ConfigFiles::Base::validate
|
215
|
+
#
|
216
|
+
def load(h, opt_h={})
|
217
|
+
opt_h_defaults = {
|
218
|
+
:compute_defaults => true,
|
219
|
+
:compute_deferred => true,
|
220
|
+
:validate => true
|
221
|
+
}
|
222
|
+
opt_h = opt_h_defaults.merge(opt_h)
|
206
223
|
|
207
224
|
h.each_pair do |id, value|
|
208
225
|
if @@parameters[id] and @@parameters[id][:converter]
|
@@ -217,16 +234,18 @@ module ConfigFiles
|
|
217
234
|
end
|
218
235
|
end
|
219
236
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
237
|
+
if opt_h[:defaults]
|
238
|
+
# assign default values to the remaining params
|
239
|
+
@@parameters.each_pair do |name, h|
|
240
|
+
if !@data[name] and @@parameters[name][:default]
|
241
|
+
@data[name] = @@parameters[name][:default]
|
242
|
+
end
|
224
243
|
end
|
225
244
|
end
|
226
245
|
|
227
|
-
@data.merge! deferred_data
|
246
|
+
@data.merge! deferred_data if opt_h[:deferred]
|
228
247
|
|
229
|
-
validate
|
248
|
+
validate if opt_h[:validate]
|
230
249
|
end
|
231
250
|
|
232
251
|
# Like Hash#[], but more rigidly! Raise an Exception on unknown
|