ns-options 0.1.1 → 0.2.0

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.
@@ -27,6 +27,10 @@ class NsOptions::Option
27
27
  should "return true with a call to #required?" do
28
28
  assert_equal true, subject.required?
29
29
  end
30
+ should "allow setting the value to nil" do
31
+ subject.value = nil
32
+ assert_nil subject.value
33
+ end
30
34
  end
31
35
 
32
36
  class IsSetTest < BaseTest
@@ -40,22 +44,28 @@ class NsOptions::Option
40
44
 
41
45
  end
42
46
  @special = NsOptions::Option.new(:no_blank, @type_class)
47
+
48
+ @boolean = NsOptions::Option.new(:boolean, NsOptions::Boolean)
43
49
  end
44
50
 
45
- should "return true/false appropriately for a boolean option with true or false" do
46
- @option.value = true
47
- assert_equal true, @option.is_set?
48
- @option.value = false
51
+ should "return appropriately" do
52
+ @option.value = "abc"
49
53
  assert_equal true, @option.is_set?
50
54
  @option.value = nil
51
55
  assert_equal false, @option.is_set?
56
+ @boolean.value = true
57
+ assert_equal true, @boolean.is_set?
58
+ @boolean.value = false
59
+ assert_equal true, @boolean.is_set?
52
60
  end
53
- should "return true/false based on type class's is_set method" do
61
+
62
+ should "use the type class's is_set method if available" do
54
63
  @special.value = "not blank"
55
64
  assert_equal true, @special.is_set?
56
65
  @special.value = " "
57
66
  assert_equal false, @special.is_set?
58
67
  end
68
+
59
69
  end
60
70
 
61
71
  class EqualityOperatorTest < BaseTest
@@ -101,30 +111,30 @@ class NsOptions::Option
101
111
  end
102
112
  end
103
113
 
104
- class WithTypeClassFixnumTest < BaseTest
105
- desc "with the Fixnum as a type class (happens through dynamic writers)"
114
+ class WithSymbolTypeClasstest < BaseTest
115
+ desc "with a Symbol as a type class"
106
116
  setup do
107
- @option = NsOptions::Option.new(:something, Fixnum)
117
+ @option = NsOptions::Option.new(:something, Symbol)
108
118
  end
109
- subject{ @option }
110
119
 
111
- should "have used Integer for it's type class" do
112
- assert_equal Integer, subject.type_class
113
- end
114
- should "allow setting it's value with an integer" do
115
- new_value = 1
116
- subject.value = new_value
117
- assert_equal new_value, subject.value
118
- end
119
- should "allow setting it's value with a float and convert it" do
120
- new_value = 12.5
121
- subject.value = new_value
122
- assert_equal new_value.to_i, subject.value
120
+ should "allow setting it with any object that responds to #to_sym" do
121
+ value = "amazing"
122
+ subject.value = value
123
+ assert_equal value.to_sym, subject.value
124
+ value = :another
125
+ subject.value = value
126
+ assert_equal value, subject.value
127
+ object_class = Class.new do
128
+ def to_sym; :object_sym; end
129
+ end
130
+ value = object_class.new
131
+ subject.value = value
132
+ assert_equal object_class.new.to_sym, subject.value
123
133
  end
124
- should "allow setting it's value with a string and convert it" do
125
- new_value = "13"
126
- subject.value = new_value
127
- assert_equal new_value.to_i, subject.value
134
+ should "error on anything that doesn't define #to_sym" do
135
+ assert_raises(NoMethodError) do
136
+ subject.value = true
137
+ end
128
138
  end
129
139
  end
130
140
 
@@ -156,26 +166,6 @@ class NsOptions::Option
156
166
  end
157
167
  end
158
168
 
159
- class WithTrueFalseClassTest < BaseTest
160
- desc "with a TrueClass/FalseClass as a type class (happens with dynamic writers)"
161
- setup do
162
- @true_option = NsOptions::Option.new(:something, TrueClass)
163
- @true_option.value = true
164
- @false_option = NsOptions::Option.new(:else, FalseClass)
165
- @false_option.value = false
166
- end
167
- subject{ @true_option }
168
-
169
- should "have used NsOptions::Option::Boolean for their type class" do
170
- assert_equal NsOptions::Option::Boolean, @true_option.type_class
171
- assert_equal NsOptions::Option::Boolean, @false_option.type_class
172
- end
173
- should "return the 'true' or 'false' value instead of the NsOptions::Option::Boolean object" do
174
- assert_equal true, @true_option.value
175
- assert_equal false, @false_option.value
176
- end
177
- end
178
-
179
169
  class WithoutTypeClassTest < BaseTest
180
170
  desc "without a type class provided"
181
171
  setup do
@@ -183,33 +173,33 @@ class NsOptions::Option
183
173
  end
184
174
  subject{ @option }
185
175
 
186
- should "have default it to String" do
187
- assert_equal String, subject.type_class
176
+ should "have default it to Object" do
177
+ assert_equal Object, subject.type_class
188
178
  end
189
179
  end
190
-
180
+
191
181
  class WithAValueOfTheSameClassTest < BaseTest
192
182
  desc "with a value of the same class"
193
183
  setup do
194
184
  @class = Class.new
195
185
  @option = NsOptions::Option.new(:something, @class)
196
186
  end
197
-
187
+
198
188
  should "use the object passed to it instead of creating a new one" do
199
189
  value = @class.new
200
190
  @option.value = value
201
191
  assert_same value, @option.value
202
192
  end
203
193
  end
204
-
194
+
205
195
  class WithAValueKindOfTest < BaseTest
206
- desc "with a value is a kind of the class"
196
+ desc "with a value that is a kind of the class"
207
197
  setup do
208
198
  @class = Class.new
209
199
  @child_class = Class.new(@class)
210
200
  @option = NsOptions::Option.new(:something, @class)
211
201
  end
212
-
202
+
213
203
  should "use the object passed to it instead of creating a new one" do
214
204
  value = @child_class.new
215
205
  @option.value = value
@@ -217,4 +207,107 @@ class NsOptions::Option
217
207
  end
218
208
  end
219
209
 
210
+ class ProcHandlingTests < BaseTest
211
+ setup do
212
+ class KindOfProc < Proc; end
213
+ @a_string = "a string"
214
+ @direct_proc = Proc.new { "I can haz eval: #{@a_string}" }
215
+ @subclass_proc = KindOfProc.new { 12345 }
216
+ @direct_opt = NsOptions::Option.new(:direct, Proc)
217
+ @subclass_opt = NsOptions::Option.new(:subclass, KindOfProc)
218
+ end
219
+
220
+ end
221
+
222
+ class WithProcTypeClassTests < ProcHandlingTests
223
+ desc "with Proc as a type class"
224
+ setup do
225
+ @direct_opt.value = @direct_proc
226
+ @subclass_opt.value = @subclass_proc
227
+ end
228
+
229
+ should "allow setting it with a proc" do
230
+ assert_kind_of Proc, @direct_opt.value
231
+ assert_kind_of KindOfProc, @subclass_opt.value
232
+ assert_equal @direct_proc, @direct_opt.value
233
+ assert_equal @subclass_proc, @subclass_opt.value
234
+ end
235
+
236
+ end
237
+
238
+ class WithLazyProcTests < ProcHandlingTests
239
+ desc "with a Proc value but no Proc-ancestor type class"
240
+ setup do
241
+ @string_opt = NsOptions::Option.new(:string, String)
242
+ end
243
+
244
+ should "set the Proc and coerce the Proc return val when read" do
245
+ @string_opt.value = @direct_proc
246
+ assert_kind_of String, @string_opt.value
247
+ assert_equal "I can haz eval: a string", @string_opt.value
248
+
249
+ @a_string = "a new string"
250
+ assert_equal "I can haz eval: a new string", @string_opt.value
251
+
252
+ @string_opt.value = @subclass_proc
253
+ assert_kind_of String, @string_opt.value
254
+ assert_equal "12345", @string_opt.value
255
+ end
256
+ end
257
+
258
+ class WithArgsTest < BaseTest
259
+ desc "with args rule"
260
+ setup do
261
+ @class = Class.new do
262
+ attr_accessor :args
263
+ def initialize(*args)
264
+ self.args = args
265
+ end
266
+ end
267
+ @value = "amazing"
268
+ end
269
+
270
+ class AsArrayTest < WithArgsTest
271
+ desc "as an array"
272
+ setup do
273
+ @args = [ true, false, { :hash => "yes" } ]
274
+ @option = NsOptions::Option.new(:something, @class, { :args => @args })
275
+ @option.value = @value
276
+ end
277
+
278
+ should "pass the args to the type class with the value" do
279
+ expected = @args.dup.insert(0, @value)
280
+ assert_equal expected, subject.value.args
281
+ end
282
+ end
283
+
284
+ class AsSingleValueTest < WithArgsTest
285
+ desc "as a single value"
286
+ setup do
287
+ @args = lambda{ "something" }
288
+ @option = NsOptions::Option.new(:something, @class, { :args => @args })
289
+ @option.value = @value
290
+ end
291
+
292
+ should "pass the single value to the type class with the value" do
293
+ expected = [*@args].insert(0, @value)
294
+ assert_equal expected, subject.value.args
295
+ end
296
+ end
297
+
298
+ class AsNilValueTest < WithArgsTest
299
+ desc "as a nil value"
300
+ setup do
301
+ @args = nil
302
+ @option = NsOptions::Option.new(:something, @class, { :args => @args })
303
+ @option.value = @value
304
+ end
305
+
306
+ should "just pass the value to the type class" do
307
+ expected = [@value]
308
+ assert_equal expected, subject.value.args
309
+ end
310
+ end
311
+ end
312
+
220
313
  end
@@ -9,9 +9,9 @@ class NsOptions::Options
9
9
  end
10
10
  subject{ @options }
11
11
 
12
- should have_accessors :key, :parent, :children
13
- should have_instance_method :namespaces, :add, :del, :remove, :get, :set, :fetch, :is_defined?,
14
- :parent_options
12
+ should have_accessors :key, :parent, :namespaces
13
+ should have_instance_method :add, :del, :remove, :get, :set, :fetch, :is_defined?,
14
+ :add_namespace, :get_namespace, :build_from
15
15
 
16
16
  should "be a kind of Hash" do
17
17
  assert_kind_of Hash, subject
@@ -31,7 +31,7 @@ class NsOptions::Options
31
31
  assert_nil subject.parent
32
32
  end
33
33
  should "return a kind of NsOption::Namespaces with a call to #children" do
34
- assert_kind_of NsOptions::Namespaces, subject.children
34
+ assert_kind_of NsOptions::Namespaces, subject.namespaces
35
35
  end
36
36
  end
37
37
 
@@ -44,20 +44,20 @@ class NsOptions::Options
44
44
  end
45
45
  subject{ @options }
46
46
 
47
- should "have added a string option on itself when adding :my_string" do
47
+ should "have added a object option on itself when adding :my_string" do
48
48
  assert(option = subject[:my_string])
49
- assert_equal String, option.type_class
50
- assert_equal({}, option.rules)
49
+ assert_equal Object, option.type_class
50
+ assert_equal({ :args => [] }, option.rules)
51
51
  end
52
52
  should "have added an integer option on itself when adding :my_integer" do
53
53
  assert(option = subject[:my_integer])
54
54
  assert_equal Integer, option.type_class
55
- assert_equal({}, option.rules)
55
+ assert_equal({ :args => [] }, option.rules)
56
56
  end
57
57
  should "have added a float option on itself when adding :my_float" do
58
58
  assert(option = subject[:my_float])
59
59
  assert_equal Float, option.type_class
60
- assert_equal({ :default => 1.0 }, option.rules)
60
+ assert_equal(1.0, option.rules[:default])
61
61
  end
62
62
  end
63
63
 
@@ -86,21 +86,6 @@ class NsOptions::Options
86
86
  should "have returned the option's value" do
87
87
  assert_equal @value, subject
88
88
  end
89
-
90
- class WithParentTest < GetTest
91
- desc "with a parent"
92
- setup do
93
- @parent = NsOptions::Namespace.new(:child)
94
- @parent.options = @options
95
- @options = NsOptions::Options.new(:child, @parent)
96
- @result = @options.get(:my_string)
97
- end
98
- subject{ @result }
99
-
100
- should "have returned the parent's option's value" do
101
- assert_equal @value, subject
102
- end
103
- end
104
89
  end
105
90
 
106
91
  class SetTest < BaseTest
@@ -116,36 +101,6 @@ class NsOptions::Options
116
101
  end
117
102
  end
118
103
 
119
- class FetchTest < BaseTest
120
- desc "fetch method"
121
- setup do
122
- option = @options.add(:my_string)
123
- @result = @options.fetch(:my_string)
124
- end
125
- subject{ @result }
126
-
127
- should "return the option definition for my_string" do
128
- assert_kind_of NsOptions::Option, subject
129
- assert_equal "my_string", subject.name
130
- end
131
-
132
- class WithAParentTest < FetchTest
133
- desc "with a parent"
134
- setup do
135
- @parent = NsOptions::Namespace.new(:child)
136
- @parent.options = @options
137
- @options = NsOptions::Options.new(:child, @parent)
138
- @result = @options.fetch(:my_string)
139
- end
140
- subject{ @result }
141
-
142
- should "return the option definition for my_string from it's parent" do
143
- assert_kind_of NsOptions::Option, subject
144
- assert_equal "my_string", subject.name
145
- end
146
- end
147
- end
148
-
149
104
  class IsDefinedTest < BaseTest
150
105
  desc "fetch method"
151
106
  setup do
@@ -159,41 +114,6 @@ class NsOptions::Options
159
114
  should "return false for an undefined option" do
160
115
  assert_equal false, subject.is_defined?(:undefined)
161
116
  end
162
-
163
- class WithAParentTest < IsDefinedTest
164
- desc "with a parent"
165
- setup do
166
- @parent = NsOptions::Namespace.new(:child)
167
- @parent.options = @options
168
- @options = NsOptions::Options.new(:child, @parent)
169
- end
170
-
171
- should "return true for an option defined on it's parent" do
172
- assert_equal true, subject.is_defined?(:my_string)
173
- end
174
- end
175
- end
176
-
177
- class ParentOptionsTest < BaseTest
178
- desc "parent_options method"
179
- subject{ @options }
180
-
181
- should "return nil" do
182
- assert_nil subject.parent_options
183
- end
184
-
185
- class WithAParentTest < ParentOptionsTest
186
- desc "with a parent"
187
- setup do
188
- @parent = NsOptions::Namespace.new(:child)
189
- @parent.options = @options
190
- @options = NsOptions::Options.new(:child, @parent)
191
- end
192
-
193
- should "return it's parent's options" do
194
- assert_equal @parent.options, subject.parent_options
195
- end
196
- end
197
117
  end
198
118
 
199
119
  class RequiredSetTest < BaseTest
@@ -223,5 +143,79 @@ class NsOptions::Options
223
143
  assert_equal true, subject.required_set?
224
144
  end
225
145
  end
146
+
147
+ class WithNamespaceTest < BaseTest
148
+ setup do
149
+ @namespace = @options.add_namespace(:something, :something)
150
+ end
151
+ end
152
+
153
+ class AddNamespaceTest < WithNamespaceTest
154
+ desc "add_namespace method"
155
+ subject{ @options }
156
+
157
+ should "create a new namespace and add it to the options namespaces collection" do
158
+ assert_instance_of NsOptions::Namespace, @namespace
159
+ assert_equal @namespace, subject.namespaces[:something]
160
+ end
161
+ end
162
+
163
+ class GetNamespaceTest < WithNamespaceTest
164
+ desc "get_namespace method"
165
+ setup do
166
+ @got_namespace = @options.get_namespace(:something)
167
+ end
168
+ subject{ @got_namespace }
169
+
170
+ should "allow retrieving a namespace without having to access the namespaces directly" do
171
+ assert_equal @namespace, subject
172
+ end
173
+ end
174
+
175
+ class IsNamespaceDefinedTest < WithNamespaceTest
176
+ desc "is_namespace_defined? method"
177
+ setup do
178
+ @has_something = @options.is_namespace_defined?(:something)
179
+ @has_nothing = @options.is_namespace_defined?(:nothing)
180
+ end
181
+
182
+ should "return a boolean of whether or not the namespace is defined" do
183
+ assert_equal true, @has_something
184
+ assert_equal false, @has_nothing
185
+ end
186
+ end
187
+
188
+ class BuildFromTest < BaseTest
189
+ desc "build_from method"
190
+ setup do
191
+ @namespace = NsOptions::Namespace.new("something")
192
+ @from = NsOptions::Options.new(:something)
193
+ @from.add(:root)
194
+ @from.add_namespace(:else) do
195
+ option :stage
196
+ end
197
+ @options = @namespace.options
198
+ @options.build_from(@from, @namespace)
199
+ end
200
+ subject{ @options }
201
+
202
+ should "have copied the options" do
203
+ @from.each do |key, from_option|
204
+ option = subject[key]
205
+ assert_equal from_option.name, option.name
206
+ assert_equal from_option.type_class, option.type_class
207
+ assert_equal from_option.rules, option.rules
208
+ assert_not_same from_option, option
209
+ end
210
+ end
211
+ should "have copied the namespaces" do
212
+ @from.namespaces.each do |name, from_namespace|
213
+ namespace = subject.get_namespace(name)
214
+ assert_equal from_namespace.options.key, namespace.options.key
215
+ assert_equal from_namespace.options.parent, namespace.options.parent
216
+ assert_not_same from_namespace, namespace
217
+ end
218
+ end
219
+ end
226
220
 
227
221
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ns-options
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Collin Redding
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-26 00:00:00 Z
18
+ date: 2011-11-28 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :development
@@ -25,11 +25,11 @@ dependencies:
25
25
  requirements:
26
26
  - - ~>
27
27
  - !ruby/object:Gem::Version
28
- hash: 7
28
+ hash: 5
29
29
  segments:
30
30
  - 0
31
- - 6
32
- version: "0.6"
31
+ - 7
32
+ version: "0.7"
33
33
  version_requirements: *id001
34
34
  name: assert
35
35
  - !ruby/object:Gem::Dependency
@@ -62,25 +62,30 @@ files:
62
62
  - README.markdown
63
63
  - Rakefile
64
64
  - lib/ns-options.rb
65
+ - lib/ns-options/boolean.rb
66
+ - lib/ns-options/errors/invalid_name.rb
65
67
  - lib/ns-options/has_options.rb
66
68
  - lib/ns-options/helper.rb
69
+ - lib/ns-options/helper/advisor.rb
67
70
  - lib/ns-options/namespace.rb
68
71
  - lib/ns-options/namespaces.rb
69
72
  - lib/ns-options/option.rb
70
- - lib/ns-options/option/boolean.rb
71
73
  - lib/ns-options/options.rb
72
74
  - lib/ns-options/version.rb
75
+ - log/.gitkeep
73
76
  - ns-options.gemspec
74
77
  - test/helper.rb
75
78
  - test/integration/app_test.rb
76
79
  - test/integration/user_test.rb
80
+ - test/irb.rb
77
81
  - test/support/app.rb
78
82
  - test/support/user.rb
83
+ - test/unit/ns-options/boolean_test.rb
79
84
  - test/unit/ns-options/has_options_test.rb
85
+ - test/unit/ns-options/helper/advisor_test.rb
80
86
  - test/unit/ns-options/helper_test.rb
81
87
  - test/unit/ns-options/namespace_test.rb
82
88
  - test/unit/ns-options/namespaces_test.rb
83
- - test/unit/ns-options/option/boolean_test.rb
84
89
  - test/unit/ns-options/option_test.rb
85
90
  - test/unit/ns-options/options_test.rb
86
91
  homepage:
@@ -120,12 +125,14 @@ test_files:
120
125
  - test/helper.rb
121
126
  - test/integration/app_test.rb
122
127
  - test/integration/user_test.rb
128
+ - test/irb.rb
123
129
  - test/support/app.rb
124
130
  - test/support/user.rb
131
+ - test/unit/ns-options/boolean_test.rb
125
132
  - test/unit/ns-options/has_options_test.rb
133
+ - test/unit/ns-options/helper/advisor_test.rb
126
134
  - test/unit/ns-options/helper_test.rb
127
135
  - test/unit/ns-options/namespace_test.rb
128
136
  - test/unit/ns-options/namespaces_test.rb
129
- - test/unit/ns-options/option/boolean_test.rb
130
137
  - test/unit/ns-options/option_test.rb
131
138
  - test/unit/ns-options/options_test.rb
@@ -1,27 +0,0 @@
1
- module NsOptions
2
- class Option
3
-
4
- class Boolean
5
- attr_accessor :actual
6
-
7
- def initialize(value)
8
- self.actual = value
9
- end
10
-
11
- def actual=(new_value)
12
- @actual = self.convert(new_value)
13
- end
14
-
15
- protected
16
-
17
- def convert(value)
18
- if [ nil, 0, '0', false, 'false' ].include?(value)
19
- false
20
- elsif value
21
- true
22
- end
23
- end
24
- end
25
-
26
- end
27
- end