option_list 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDA0MjBiNDdkNWRhMzgzMzBkYWJiMjhiOGUxNTA3YmE5ZjI3Y2Q4OA==
4
+ OWQxY2I0ZTM2NjM1OTBiZDU2MWNmZDk4MjcyMDQ1ZGUzYWQzNDcyMA==
5
5
  data.tar.gz: !binary |-
6
- MmIzZGRjZDc0YWRlYTgxNWUyY2VjMWI2MTBmNjg3NGIzZjFlMmMzMg==
6
+ NDUyZWQxNmQ5MzMwZTkzZThhZTFjNzhjMTNmN2Y4ZmY1NDU2N2RmNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MWE0ZjhiZmU0ZmMxYmUzY2VjMDFiZjM5MDU4MjQwODg2NjUwODExNzk0NWVh
10
- NGU2ZmQ1OGU3NzRkOGQ1ZTY2ZDI2ODg2NWU3OGQ1YWU1N2E2ZGZlYTQzZWRm
11
- NTUyYTdmM2VjYTVlOTcwYTFhODdjNTc4NzBhYjk5MDQyZWQ1MTU=
9
+ ZGNhMjA2NjExMzNjNDAzNzdjNmZmODJkODg1NmQwZDYzOTg2MTkxNTFiMDUx
10
+ MmU1NGFkMTMzZGE1MjI1MzU3MWE2M2U3MzEwMDEwYWYyNDI0YmIzYjk4MGQx
11
+ NDM3NjEwNTEwYzkxY2YxNjY5MWJmZjc0NDY3NmU3MmMyZmE4NGU=
12
12
  data.tar.gz: !binary |-
13
- NmRiN2NmMjM3OGFkYjcwMThjNjc5MzU1MjM3ODlkODIzNGE4YzZhMTliYzhh
14
- YWNkZTNhNjE4MDgwZTc2NzE0Y2QzNzkzOWIwYTJiOWQzMmU0NGEzOTI1MTFl
15
- ZTUxZTAwOTAzZTc0YjBmMTYyNTEyOGMxODI4NTFmYjI5ZWE2ZmI=
13
+ ZDViOTVkN2U5NjMyZDNlYjliZDdlODk2NTM2MzFiZTJmMDExOThjNzhhOTMw
14
+ MTM1Yjk5ZTEwNjBiYTE2NmQzNzRkYWI4OWIyM2JhNGIwMGY2NzRiNGE2Zjli
15
+ MGJmMjMyYjliNDAzNmVmYmY2MGM1MzBmMzg1ODE1NGEwMDgzZTM=
@@ -38,11 +38,15 @@ require 'set'
38
38
  #\OptionList class itself, it is reasonable to assume that a function option
39
39
  #handler could very easily end up embedded in such an environment. In fact, my
40
40
  #first major test of the class ran into this exact issue.
41
+ #=== Version 1.1.0
42
+ #Added a default value of false to signify that no default exists for a
43
+ #mandatory parameter.
44
+ #Modified processing of array specs to avoid side effects in the parameters.
41
45
  class OptionList
42
46
 
43
47
  #The option list code version.
44
48
  def self.version
45
- '1.0.1'
49
+ '1.1.0'
46
50
  end
47
51
 
48
52
  #The option list code version. This is a redirect to the class method.
@@ -60,8 +64,12 @@ class OptionList
60
64
  #In this form of specification, the first element of the array is the
61
65
  #category of the option, and the second and following entries are the
62
66
  #allowed values for that category. The second element has a special role. It
63
- #is the default value for the category. Only this value may be nil to have
64
- #no default value. The symbols used in each category must be unique.
67
+ #is the default value for the category. This value may be one of:
68
+ #* A symbol - The default symbolic value for this option.
69
+ #* nil - The default value is nil and this setting is optional.
70
+ #* false - There is no default value and this setting is mandatory.
71
+ #The symbols used in each category must be unique across
72
+ #all of the categories in the option list.
65
73
  #==== Hash Specification
66
74
  #Hash specifications are used to specify options that do not have a fixed set
67
75
  #of possible values. In this form of specification, the hash key symbol is
@@ -89,6 +97,7 @@ class OptionList
89
97
  #* ArgumentError for a number of invalid argument conditions.
90
98
  def initialize(*option_specs, &select_block)
91
99
  fAE "Missing option specifications." if option_specs.empty?
100
+ @mandatory = Array.new
92
101
  @categories = Hash.new
93
102
  @default = Hash.new
94
103
 
@@ -118,10 +127,9 @@ class OptionList
118
127
  #Some examples:
119
128
  # o = foo(:sedan, :turbo)
120
129
  # o = foo({:style=>:sedan})
121
- # o = foo({:page_len=>60})
122
- # o = foo(:sedan, :turbo, {:page_len=>60})
123
- # o = foo({:style=>:sedan, :page_len=>60})
124
- # o = foo({:style=>:sedan}, {:page_len=>60})
130
+ # o = foo(page_len: 60)
131
+ # o = foo(:sedan, :turbo, page_len: 60)
132
+ # o = foo(style: :sedan, page_len: 60)
125
133
  #=== Passing in option data:
126
134
  #The caller of this method may pass these options in a number of ways:
127
135
  #==== Array (via a splat)
@@ -144,6 +152,7 @@ class OptionList
144
152
  #==== Hash
145
153
  #Options may also be passed via a hash.
146
154
  # test(34, 53, 76, 'hike!', {:history=>:history, :page_len=>55})
155
+ # test(34, 53, 76, 'hike!', history: :history, page_len: 55)
147
156
  #==== Symbol
148
157
  #If only a single option is required, it can be passed simply as:
149
158
  # test(34, 53, 76, 'hike!', :history)
@@ -152,7 +161,7 @@ class OptionList
152
161
  # with the splat operator. Note that if select is called with no arguments,
153
162
  # all of the default values will be selected.
154
163
  #==== Returns:
155
- #An option value object wich is a singleton subclass of the Hash class.
164
+ #An option value object which is a singleton subclass of the Hash class.
156
165
  #==== Exceptions:
157
166
  #* ArgumentError for a number of invalid argument conditions.
158
167
  #==== Notes:
@@ -173,6 +182,10 @@ class OptionList
173
182
  end
174
183
  end
175
184
 
185
+ @mandatory.each do |cat|
186
+ fAE "Missing mandatory setting #{cat}" unless selected[cat]
187
+ end
188
+
176
189
  @select_block.call(selected) unless @select_block.nil?
177
190
  selected
178
191
  end
@@ -187,14 +200,16 @@ class OptionList
187
200
  #Process an array spec that lists all the valid values for an option. See
188
201
  #the new method for more information on these specs.
189
202
  def array_spec(spec)
190
- cat = spec.delete_at(0)
203
+ cat = spec[0]
204
+ spec = spec[1...spec.length]
205
+
191
206
  fAE "Found #{cat.class}, expected Symbol." unless cat.is_a?(Symbol)
192
207
  fAE "Duplicate category: #{cat}" if @default.has_key?(cat)
193
208
  fAE "Invalid number of entries for #{cat}." unless spec.length > 1
194
209
  @default.define_singleton_method(cat) { self[cat] }
195
-
210
+
196
211
  spec.each_with_index do |opt, index|
197
- if opt != nil
212
+ if opt
198
213
  fAE "Found #{opt.class}, expected Symbol." unless opt.is_a?(Symbol)
199
214
  fAE "Duplicate option: #{opt}" if @categories.has_key?(opt)
200
215
 
@@ -203,9 +218,10 @@ class OptionList
203
218
  @default.define_singleton_method(qry) { self[cat] == opt }
204
219
  @default[cat] = opt if index == 0
205
220
  elsif index == 0
206
- @default[cat] = nil
221
+ @default[cat] = opt
222
+ @mandatory << cat if opt == false
207
223
  else
208
- fAE "The value nil is only allowed as the default option."
224
+ fAE "The values nil/false are only allowed as the default option."
209
225
  end
210
226
  end
211
227
  end
@@ -222,7 +238,7 @@ class OptionList
222
238
  @default.define_singleton_method(cat) { self[cat] }
223
239
  @categories[cat] = value_entry
224
240
  @default[cat] = value
225
- end
241
+ end
226
242
  end
227
243
 
228
244
  #Process a symbolic option selection.
data/rakefile.rb CHANGED
@@ -5,11 +5,11 @@ require 'rdoc/task'
5
5
  RDoc::Task.new do |rdoc|
6
6
  rdoc.rdoc_dir = "rdoc"
7
7
  #rdoc.main = "option_list.rb"
8
- rdoc.rdoc_files = ['option_list.rb', 'option_list_test.rb', 'license.txt']
8
+ rdoc.rdoc_files = ['lib/option_list.rb', 'tests/option_list_test.rb', 'license.txt']
9
9
  rdoc.options << '--visibility' << 'private'
10
10
  end
11
11
 
12
12
  Rake::TestTask.new do |t|
13
- t.test_files = ['option_list_test.rb']
13
+ t.test_files = ['tests/option_list_test.rb']
14
14
  t.verbose = false
15
15
  end
@@ -1,7 +1,7 @@
1
1
  #A formal testing frame for the option list class.
2
2
  #Execute this file to perform the tests.
3
3
 
4
- require_relative 'option_list'
4
+ require_relative '../lib/option_list'
5
5
  require 'minitest/autorun'
6
6
 
7
7
  class OptionTest
@@ -197,6 +197,29 @@ class OptionListTester < MiniTest::Unit::TestCase
197
197
  assert(o.history.nil?)
198
198
  end
199
199
 
200
+ def test_that_mandatory_parms_work
201
+ ol2 = OptionList.new([:history, false, :history, :nohistory])
202
+
203
+ o = ol2.select([:history])
204
+ assert(o.history?)
205
+ refute(o.nohistory?)
206
+ assert_equal(o.history, :history)
207
+
208
+ o = ol2.select([:nohistory])
209
+ assert(o.nohistory?)
210
+ refute(o.history?)
211
+ assert_equal(o.history, :nohistory)
212
+
213
+ assert_raises(ArgumentError) { ol2.select() }
214
+ end
215
+
216
+ def test_that_it_does_not_munge_parms
217
+ parm1 = [:history, false, :history, :nohistory]
218
+ parm2 = parm1.clone
219
+ ol2 = OptionList.new(parm1)
220
+ assert_equal(parm1, parm2)
221
+ end
222
+
200
223
  def test_that_the_select_block_works
201
224
  ol3 = OptionList.new([:history, nil, :history, :nohistory],
202
225
  fuel1: :matter, fuel2: :antimatter) do |opt|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: option_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Camilleri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-02 00:00:00.000000000 Z
11
+ date: 2013-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -45,18 +45,18 @@ extensions: []
45
45
  extra_rdoc_files:
46
46
  - license.txt
47
47
  files:
48
- - option_list.rb
49
- - option_list_test.rb
48
+ - lib/option_list.rb
49
+ - tests/option_list_test.rb
50
50
  - rakefile.rb
51
51
  - license.txt
52
- homepage: http://teuthida-technologies.com/?p=1506
52
+ homepage: http://teuthida-technologies.com/
53
53
  licenses:
54
54
  - MIT
55
55
  metadata: {}
56
56
  post_install_message:
57
57
  rdoc_options: []
58
58
  require_paths:
59
- - .
59
+ - lib
60
60
  required_ruby_version: !ruby/object:Gem::Requirement
61
61
  requirements:
62
62
  - - ! '>='
@@ -74,4 +74,4 @@ signing_key:
74
74
  specification_version: 4
75
75
  summary: A unified handler for flexible function option parameters.
76
76
  test_files:
77
- - option_list_test.rb
77
+ - tests/option_list_test.rb