option_list 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +9 -9
- data/option_list.rb +18 -1
- data/option_list_test.rb +21 -0
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MGE1NTkyMWExYjVhYmY1MzJjZmQyOWM2ODVlNmMwNWNmNjVhM2QxYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
7
|
-
|
6
|
+
NjAyN2M4NWQ2OGRiZGRjNWY5MTU5MGE4NjMwYjUzNzYwZjNhMDI3MQ==
|
7
|
+
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzljNzk3MzI0NjM5ZTM5NmZiZDY4YjlmMWUzNThiODc5ZjJmZmE5NjliMTk0
|
10
|
+
NGQ3ZThjNThiNTE0ZTExNGVkYmRlMGZkNzYyNDcxZjcwOWZiMTAxMjIzYmI5
|
11
|
+
YmI1OWZjNjM4MjFjNDJkMTVjM2EwNzc3NDExNDc3OWZjMTJiMTk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
M2E4MmM2NjdjNjQ4MDA0ZmRjNDcyZGE1YTVmZjU0NmY4NTA4MDcxZDJkOGYy
|
14
|
+
YWI5ODBiNTMzNTU4ZjIxZTQzMjI1YjNmNTkwNjc0ZDAwNWQ5ODJhOGFlNzFi
|
15
|
+
YjRmMTkyYThhYTdmNDYwNThhM2ZiNDhjYWNiNTA3ODg3MzI4MjM=
|
data/option_list.rb
CHANGED
@@ -69,6 +69,10 @@ class OptionList
|
|
69
69
|
#==== Parameters:
|
70
70
|
#* option_specs - The comma separated option specs, made into an array by the
|
71
71
|
# splat operator.
|
72
|
+
#* select_block - An optional block of code that is called when selections
|
73
|
+
# have been made. This allows for custom validations to be applied at that
|
74
|
+
# point. This block should accept one argument, a reference to the option
|
75
|
+
# list object that is calling it for validation.
|
72
76
|
#==== Dynamic Methods:
|
73
77
|
#As option specs are added, new methods are created to allow for easy access
|
74
78
|
#to the option information. If the above example were processed, the following
|
@@ -79,11 +83,12 @@ class OptionList
|
|
79
83
|
#* page_len - would return the value of the :page_len category.
|
80
84
|
#==== Exceptions:
|
81
85
|
#* ArgumentError for a number of invalid argument conditions.
|
82
|
-
def initialize(*option_specs)
|
86
|
+
def initialize(*option_specs, &select_block)
|
83
87
|
@categories = Hash.new
|
84
88
|
@default = Hash.new
|
85
89
|
@selected = Hash.new
|
86
90
|
do_add_specs(option_specs)
|
91
|
+
@select_block = select_block
|
87
92
|
end
|
88
93
|
|
89
94
|
#Add additonal option specifications to the option list. See the method new
|
@@ -198,9 +203,13 @@ class OptionList
|
|
198
203
|
# all of the default values will be selected.
|
199
204
|
#==== Exceptions:
|
200
205
|
#* ArgumentError for a number of invalid argument conditions.
|
206
|
+
#==== Notes:
|
207
|
+
#After processing the selections, the selection block is called if one was
|
208
|
+
#defined in by the constructor.
|
201
209
|
def select(selections=[])
|
202
210
|
@selected = @default.clone
|
203
211
|
add_selections(selections)
|
212
|
+
@select_block.call(self) unless @select_block.nil?
|
204
213
|
end
|
205
214
|
|
206
215
|
#Add/override additional selections to the option_list. See the select method
|
@@ -220,6 +229,12 @@ class OptionList
|
|
220
229
|
end
|
221
230
|
end
|
222
231
|
|
232
|
+
#Call the selection block if one was defined in by the constructor. This
|
233
|
+
#method must be used if selections are added with add_selections.
|
234
|
+
def validate
|
235
|
+
@select_block.call(self) unless @select_block.nil?
|
236
|
+
end
|
237
|
+
|
223
238
|
private #Some more privacy, please!
|
224
239
|
|
225
240
|
#Process a symbolic option selection.
|
@@ -252,6 +267,8 @@ class OptionList
|
|
252
267
|
@selected[cat] = value
|
253
268
|
end
|
254
269
|
end
|
270
|
+
|
271
|
+
public
|
255
272
|
|
256
273
|
#Fail with an argument error.
|
257
274
|
def fAE(msg)
|
data/option_list_test.rb
CHANGED
@@ -5,9 +5,25 @@ require_relative 'option_list'
|
|
5
5
|
require 'minitest/autorun'
|
6
6
|
|
7
7
|
class OptionListTester < MiniTest::Unit::TestCase
|
8
|
+
$do_this_only_one_time = true
|
9
|
+
|
10
|
+
def initialize(*all)
|
11
|
+
if $do_this_only_one_time
|
12
|
+
puts "Testing file: option_list.rb"
|
13
|
+
$do_this_only_one_time = false
|
14
|
+
end
|
15
|
+
|
16
|
+
super(*all)
|
17
|
+
end
|
18
|
+
|
8
19
|
def setup
|
9
20
|
@ol1 = OptionList.new([:history, :history, :nohistory], {:pg_len => 42})
|
10
21
|
@ol2 = OptionList.new([:history, nil, :history, :nohistory])
|
22
|
+
@ol3 = OptionList.new([:history, nil, :history, :nohistory],
|
23
|
+
{:fuel1=>:matter, :fuel2=>:antimatter}) do |opt|
|
24
|
+
opt.fAE "The :history option must be set." if opt.history.nil?
|
25
|
+
opt.fAE "Improper fuel mix." unless opt.fuel1 == :matter && opt.fuel2 == :antimatter
|
26
|
+
end
|
11
27
|
end
|
12
28
|
|
13
29
|
def test_that_it_rejects_bad_specs
|
@@ -241,4 +257,9 @@ class OptionListTester < MiniTest::Unit::TestCase
|
|
241
257
|
refute(@ol2.nohistory?)
|
242
258
|
assert(@ol2.history.nil?)
|
243
259
|
end
|
260
|
+
|
261
|
+
def test_that_the_select_block_works
|
262
|
+
assert_raises(ArgumentError) { @ol3.select() }
|
263
|
+
assert_raises(ArgumentError) { @ol3.select({:history=>:nohistory, :fuel2=>:matter}) }
|
264
|
+
end
|
244
265
|
end
|
metadata
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: option_list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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-09-
|
11
|
+
date: 2013-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: A unified handler for flexible function option parameters.
|
13
|
+
description: A unified handler for flexible function option parameters. Please see
|
14
|
+
the homepage for docs.
|
14
15
|
email: peter.c.camilleri@gmail.com
|
15
16
|
executables: []
|
16
17
|
extensions: []
|
@@ -18,16 +19,14 @@ extra_rdoc_files: []
|
|
18
19
|
files:
|
19
20
|
- option_list.rb
|
20
21
|
- option_list_test.rb
|
21
|
-
homepage: http://teuthida-technologies.com
|
22
|
+
homepage: http://teuthida-technologies.com/?p=1506
|
22
23
|
licenses:
|
23
24
|
- MIT
|
24
25
|
metadata: {}
|
25
26
|
post_install_message:
|
26
|
-
rdoc_options:
|
27
|
-
- --output=doc
|
28
|
-
- option_list.rb
|
27
|
+
rdoc_options: []
|
29
28
|
require_paths:
|
30
|
-
-
|
29
|
+
- .
|
31
30
|
required_ruby_version: !ruby/object:Gem::Requirement
|
32
31
|
requirements:
|
33
32
|
- - ! '>='
|
@@ -41,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
40
|
requirements:
|
42
41
|
- No special requirements.
|
43
42
|
rubyforge_project:
|
44
|
-
rubygems_version: 2.
|
43
|
+
rubygems_version: 2.1.4
|
45
44
|
signing_key:
|
46
45
|
specification_version: 4
|
47
46
|
summary: A unified handler for flexible function option parameters.
|