parameters 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/README.txt +1 -1
- data/lib/parameters.rb +1 -0
- data/lib/parameters/class_param.rb +20 -1
- data/lib/parameters/instance_param.rb +13 -1
- data/lib/parameters/param.rb +1 -1
- data/lib/parameters/parameters.rb +44 -11
- data/lib/parameters/version.rb +1 -1
- data/spec/parameters_spec.rb +9 -1
- metadata +2 -2
data/History.txt
CHANGED
data/README.txt
CHANGED
data/lib/parameters.rb
CHANGED
@@ -10,11 +10,30 @@ module Parameters
|
|
10
10
|
# Creates a new ClassParam object with the specified _name_,
|
11
11
|
# given _description_ and _value_.
|
12
12
|
#
|
13
|
-
def initialize(name,description=
|
13
|
+
def initialize(name,description=nil,value=nil)
|
14
14
|
super(name,description)
|
15
15
|
|
16
16
|
@value = value
|
17
17
|
end
|
18
18
|
|
19
|
+
#
|
20
|
+
# Returns the String representation of the class param.
|
21
|
+
#
|
22
|
+
def to_s
|
23
|
+
text = " #{@name}"
|
24
|
+
|
25
|
+
text << " [#{@value}]" if @value
|
26
|
+
text << "\t#{@description}" if @description
|
27
|
+
|
28
|
+
return text
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# Inspects the class params value.
|
33
|
+
#
|
34
|
+
def inspect
|
35
|
+
@value.inspect
|
36
|
+
end
|
37
|
+
|
19
38
|
end
|
20
39
|
end
|
@@ -8,7 +8,7 @@ module Parameters
|
|
8
8
|
# Creates a new InstanceParam object with the specified _object_ and
|
9
9
|
# _name_, and the given _description_.
|
10
10
|
#
|
11
|
-
def initialize(object,name,description=
|
11
|
+
def initialize(object,name,description=nil)
|
12
12
|
super(name,description)
|
13
13
|
|
14
14
|
@object = object
|
@@ -28,6 +28,18 @@ module Parameters
|
|
28
28
|
@object.instance_variable_set("@#{@name}",value)
|
29
29
|
end
|
30
30
|
|
31
|
+
#
|
32
|
+
# Returns a String representation of the instance param.
|
33
|
+
#
|
34
|
+
def to_s
|
35
|
+
text = " #{@name}"
|
36
|
+
|
37
|
+
text << " [#{value}]" if value
|
38
|
+
text << "\t#{@description}" if @description
|
39
|
+
|
40
|
+
return text
|
41
|
+
end
|
42
|
+
|
31
43
|
#
|
32
44
|
# Inspects the instance params value.
|
33
45
|
#
|
data/lib/parameters/param.rb
CHANGED
@@ -126,6 +126,15 @@ module Parameters
|
|
126
126
|
def param_value(name)
|
127
127
|
get_param(name).value
|
128
128
|
end
|
129
|
+
|
130
|
+
#
|
131
|
+
# Print the class parameters to the given _output_ stream.
|
132
|
+
#
|
133
|
+
def print_params(output=STDOUT)
|
134
|
+
each_param do |param|
|
135
|
+
output.puts param
|
136
|
+
end
|
137
|
+
end
|
129
138
|
end
|
130
139
|
end
|
131
140
|
|
@@ -147,7 +156,7 @@ module Parameters
|
|
147
156
|
instance_variable_set("@#{param.name}",value)
|
148
157
|
end
|
149
158
|
|
150
|
-
params[param.name] = InstanceParam.new(self,param.name,param.description)
|
159
|
+
self.params[param.name] = InstanceParam.new(self,param.name,param.description)
|
151
160
|
end
|
152
161
|
end
|
153
162
|
|
@@ -185,7 +194,7 @@ module Parameters
|
|
185
194
|
instance_variable_set("@#{name}",options[:default])
|
186
195
|
|
187
196
|
# add the new parameter
|
188
|
-
params[name] = InstanceParam.new(self,name,options[:description])
|
197
|
+
self.params[name] = InstanceParam.new(self,name,options[:description])
|
189
198
|
|
190
199
|
instance_eval %{
|
191
200
|
# define the reader method for the parameter
|
@@ -225,11 +234,19 @@ module Parameters
|
|
225
234
|
def params=(values)
|
226
235
|
values.each do |name,value|
|
227
236
|
if has_param?(name)
|
228
|
-
|
237
|
+
self.params[name.to_sym].value = value
|
229
238
|
end
|
230
239
|
end
|
231
240
|
end
|
232
241
|
|
242
|
+
#
|
243
|
+
# Iterates over each instance parameter, passing each one to the given
|
244
|
+
# _block_.
|
245
|
+
#
|
246
|
+
def each_param(&block)
|
247
|
+
self.params.each_value(&block)
|
248
|
+
end
|
249
|
+
|
233
250
|
#
|
234
251
|
# Returns +true+ if the a parameter with the specified _name_ exists,
|
235
252
|
# returns +false+ otherwise.
|
@@ -237,7 +254,7 @@ module Parameters
|
|
237
254
|
# obj.has_param?('rhost') # => true
|
238
255
|
#
|
239
256
|
def has_param?(name)
|
240
|
-
params.has_key?(name.to_sym)
|
257
|
+
self.params.has_key?(name.to_sym)
|
241
258
|
end
|
242
259
|
|
243
260
|
#
|
@@ -253,7 +270,7 @@ module Parameters
|
|
253
270
|
raise(ParamNotFound,"parameter #{name.to_s.dump} was not found within #{self.to_s.dump}",caller)
|
254
271
|
end
|
255
272
|
|
256
|
-
return params[name]
|
273
|
+
return self.params[name]
|
257
274
|
end
|
258
275
|
|
259
276
|
#
|
@@ -277,14 +294,30 @@ module Parameters
|
|
277
294
|
end
|
278
295
|
|
279
296
|
#
|
280
|
-
#
|
297
|
+
# Print the instance parameters to the given _output_ stream.
|
298
|
+
#
|
299
|
+
def print_params(output=STDOUT)
|
300
|
+
each_param do |param|
|
301
|
+
output.puts param
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
protected
|
306
|
+
|
281
307
|
#
|
282
|
-
#
|
283
|
-
#
|
308
|
+
# Requires that the parameters with the specified _names_ have
|
309
|
+
# non +nil+ values. If a parameter with a +nil+ value is found
|
310
|
+
# a MissingParam exception will be raised.
|
284
311
|
#
|
285
|
-
def
|
286
|
-
|
287
|
-
|
312
|
+
def require_params(*names)
|
313
|
+
names.each do |name|
|
314
|
+
name = name.to_s
|
315
|
+
|
316
|
+
unless instance_variable_get("@#{name}")
|
317
|
+
raise(MissingParam,"parameter #{name.dump} has no value",caller)
|
318
|
+
end
|
288
319
|
end
|
320
|
+
|
321
|
+
return true
|
289
322
|
end
|
290
323
|
end
|
data/lib/parameters/version.rb
CHANGED
data/spec/parameters_spec.rb
CHANGED
@@ -13,6 +13,8 @@ describe Parameters do
|
|
13
13
|
:default => 'thing',
|
14
14
|
:description => 'This parameter has a default value'
|
15
15
|
|
16
|
+
parameter :var_without_default,
|
17
|
+
:description => 'This parameter does not have a default value'
|
16
18
|
end
|
17
19
|
|
18
20
|
class InheritedParameters < TestParameters
|
@@ -116,7 +118,7 @@ describe Parameters do
|
|
116
118
|
end
|
117
119
|
|
118
120
|
it "should allow for setting parameters en-mass" do
|
119
|
-
@test.
|
121
|
+
@test.params = {:var => 3, :var_with_default => 7}
|
120
122
|
|
121
123
|
@test.param_value(:var).should == 3
|
122
124
|
@test.param_value(:var_with_default).should == 7
|
@@ -125,5 +127,11 @@ describe Parameters do
|
|
125
127
|
it "should provide descriptions for parameters" do
|
126
128
|
@test.describe_param(:var).should_not be_empty
|
127
129
|
end
|
130
|
+
|
131
|
+
it "should require that certain parameters have non nil values" do
|
132
|
+
lambda {
|
133
|
+
@test.instance_eval { require_params(:var_without_default) }
|
134
|
+
}.should raise_error(Parameters::MissingParam)
|
135
|
+
end
|
128
136
|
end
|
129
137
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Postmodern
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-12-
|
12
|
+
date: 2008-12-27 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|