ns-options 1.0.0.rc3 → 1.0.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ns-options/namespace_data.rb +1 -2
- data/lib/ns-options/option.rb +2 -0
- data/lib/ns-options/version.rb +1 -1
- data/test/unit/namespace_tests.rb +18 -7
- data/test/unit/option_tests.rb +78 -13
- metadata +4 -4
@@ -131,11 +131,10 @@ module NsOptions
|
|
131
131
|
elsif is_option_reader?(dslm)
|
132
132
|
get_option(dslm.name)
|
133
133
|
elsif is_option_writer?(dslm)
|
134
|
-
# TODO: remove same-named opt/ns when adding the other with same name
|
135
134
|
add_option(dslm.name) unless has_option?(dslm.name)
|
136
135
|
begin
|
137
136
|
set_option(dslm.name, dslm.data)
|
138
|
-
rescue NsOptions::Option::CoerceError
|
137
|
+
rescue NsOptions::Option::CoerceError => err
|
139
138
|
error! bt, err # reraise this exception with a sane backtrace
|
140
139
|
end
|
141
140
|
else # namespace writer or unknown
|
data/lib/ns-options/option.rb
CHANGED
@@ -93,6 +93,8 @@ module NsOptions
|
|
93
93
|
if [ ::Integer, ::Float, ::String ].include?(self.type_class)
|
94
94
|
# ruby type conversion, i.e. String(1)
|
95
95
|
Object.send(self.type_class.to_s, value)
|
96
|
+
elsif self.type_class == ::Fixnum
|
97
|
+
Object.send('Integer', value)
|
96
98
|
elsif self.type_class == ::Symbol
|
97
99
|
value.to_sym
|
98
100
|
elsif self.type_class == ::Array
|
data/lib/ns-options/version.rb
CHANGED
@@ -44,7 +44,7 @@ class NsOptions::Namespace
|
|
44
44
|
class OptionTests < BaseTests
|
45
45
|
desc "when adding an option named `something`"
|
46
46
|
setup do
|
47
|
-
@added_opt = @namespace.option('something',
|
47
|
+
@added_opt = @namespace.option('something', Fixnum, { :default => 1 })
|
48
48
|
end
|
49
49
|
|
50
50
|
should "have added an option to the namespace" do
|
@@ -52,8 +52,8 @@ class NsOptions::Namespace
|
|
52
52
|
|
53
53
|
opt = subject.__data__.child_options[:something]
|
54
54
|
assert_equal 'something', opt.name
|
55
|
-
assert_equal
|
56
|
-
assert_equal
|
55
|
+
assert_equal Fixnum, opt.type_class
|
56
|
+
assert_equal 1, opt.rules[:default]
|
57
57
|
end
|
58
58
|
|
59
59
|
should "return the option it added" do
|
@@ -86,13 +86,24 @@ class NsOptions::Namespace
|
|
86
86
|
end
|
87
87
|
|
88
88
|
should "be writable through the defined writer" do
|
89
|
-
assert_nothing_raised{ subject.something =
|
90
|
-
assert_equal
|
89
|
+
assert_nothing_raised{ subject.something = 2 }
|
90
|
+
assert_equal 2, subject.something
|
91
91
|
end
|
92
92
|
|
93
93
|
should "be writable through the reader with args" do
|
94
|
-
assert_nothing_raised{ subject.something
|
95
|
-
assert_equal
|
94
|
+
assert_nothing_raised{ subject.something 3 }
|
95
|
+
assert_equal 3, subject.something
|
96
|
+
end
|
97
|
+
|
98
|
+
should "raise CoerceError when writing values not coercable to the type class" do
|
99
|
+
err = begin
|
100
|
+
subject.something = "can't be coerced to a Fixnum!"
|
101
|
+
rescue Exception => err
|
102
|
+
err
|
103
|
+
end
|
104
|
+
|
105
|
+
assert_equal NsOptions::Option::CoerceError, err.class
|
106
|
+
assert_included 'test/unit/namespace_tests.rb:', err.backtrace.first
|
96
107
|
end
|
97
108
|
|
98
109
|
end
|
data/test/unit/option_tests.rb
CHANGED
@@ -190,29 +190,94 @@ class NsOptions::Option
|
|
190
190
|
|
191
191
|
end
|
192
192
|
|
193
|
-
class
|
194
|
-
desc "with
|
193
|
+
class WithIntegerTypeClassTests < BaseTests
|
194
|
+
desc "with an Integer type class"
|
195
|
+
setup do
|
196
|
+
@option = NsOptions::Option.new(:something, Integer)
|
197
|
+
end
|
198
|
+
subject{ @option }
|
199
|
+
|
200
|
+
should "allow setting it's value" do
|
201
|
+
subject.value = 12
|
202
|
+
assert_equal 12, subject.value
|
203
|
+
end
|
204
|
+
|
205
|
+
should "allow setting it's value with a string and convert it" do
|
206
|
+
subject.value = "13"
|
207
|
+
assert_equal "13".to_i, subject.value
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
class WithFixnumTypeClassTests < BaseTests
|
213
|
+
desc "with a Fixnum type class"
|
214
|
+
setup do
|
215
|
+
@option = NsOptions::Option.new(:something, Fixnum)
|
216
|
+
end
|
217
|
+
subject{ @option }
|
218
|
+
|
219
|
+
should "allow setting it's value" do
|
220
|
+
subject.value = 12
|
221
|
+
assert_equal 12, subject.value
|
222
|
+
end
|
223
|
+
|
224
|
+
should "allow setting it's value with a string and convert it" do
|
225
|
+
subject.value = "13"
|
226
|
+
assert_equal "13".to_i, subject.value
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
class WithFloatTypeClassTests < BaseTests
|
232
|
+
desc "with a Float type class"
|
195
233
|
setup do
|
196
234
|
@option = NsOptions::Option.new(:something, Float)
|
197
235
|
end
|
198
236
|
subject{ @option }
|
199
237
|
|
200
|
-
should "allow setting it's value
|
201
|
-
|
202
|
-
subject.value
|
203
|
-
assert_equal new_value, subject.value
|
238
|
+
should "allow setting it's value" do
|
239
|
+
subject.value = 12.5
|
240
|
+
assert_equal 12.5, subject.value
|
204
241
|
end
|
205
242
|
|
206
243
|
should "allow setting it's value with a string and convert it" do
|
207
|
-
|
208
|
-
subject.value
|
209
|
-
assert_equal new_value.to_f, subject.value
|
244
|
+
subject.value = "13.4"
|
245
|
+
assert_equal "13.4".to_f, subject.value
|
210
246
|
end
|
211
247
|
|
212
248
|
should "allow setting it's value with an integer and convert it" do
|
213
|
-
|
214
|
-
subject.value
|
215
|
-
|
249
|
+
subject.value = 1
|
250
|
+
assert_equal 1.to_f, subject.value
|
251
|
+
end
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
class WithStringTypeClassTests < BaseTests
|
256
|
+
desc "with an String type class"
|
257
|
+
setup do
|
258
|
+
@option = NsOptions::Option.new(:something, String)
|
259
|
+
end
|
260
|
+
subject{ @option }
|
261
|
+
|
262
|
+
should "allow setting it's value" do
|
263
|
+
subject.value = "12"
|
264
|
+
assert_equal "12", subject.value
|
265
|
+
end
|
266
|
+
|
267
|
+
should "allow setting it's value with a numeric and convert it" do
|
268
|
+
subject.value = 13
|
269
|
+
assert_equal 13.to_s, subject.value
|
270
|
+
|
271
|
+
subject.value = 13.5
|
272
|
+
assert_equal 13.5.to_s, subject.value
|
273
|
+
end
|
274
|
+
|
275
|
+
should "allow setting it's value with other things and convert it" do
|
276
|
+
subject.value = true
|
277
|
+
assert_equal true.to_s, subject.value
|
278
|
+
|
279
|
+
subject.value = NsOptions
|
280
|
+
assert_equal NsOptions.to_s, subject.value
|
216
281
|
end
|
217
282
|
|
218
283
|
end
|
@@ -300,7 +365,7 @@ class NsOptions::Option
|
|
300
365
|
@option = NsOptions::Option.new(:something, SuperSuperTestTest)
|
301
366
|
end
|
302
367
|
|
303
|
-
should "reraise
|
368
|
+
should "reraise as a CoerceError with a custom message and backtrace" do
|
304
369
|
err = begin
|
305
370
|
@option.value = "arg error should be raised"
|
306
371
|
rescue Exception => err
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ns-options
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: -1768309136
|
5
5
|
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
9
|
- 0
|
10
10
|
- rc
|
11
|
-
-
|
12
|
-
version: 1.0.0.
|
11
|
+
- 4
|
12
|
+
version: 1.0.0.rc4
|
13
13
|
platform: ruby
|
14
14
|
authors:
|
15
15
|
- Collin Redding
|
@@ -18,7 +18,7 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2012-
|
21
|
+
date: 2012-12-03 00:00:00 Z
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
24
24
|
name: assert
|