libclimate-ruby 0.2.4 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11f0e8e0239a415d2c396a07c97b040db5bc29c3
4
- data.tar.gz: 9fd775fe9b0e5c64c0507d6fdb76a2208889a35a
3
+ metadata.gz: 8ddeb7a8f3fbcbe387ff00dc9c0413a2921a1573
4
+ data.tar.gz: 205642b3c90c1c34685163a87534b496e10b1e97
5
5
  SHA512:
6
- metadata.gz: cdefc270711e796b5e26bf9e436ef44cddfe309a1120cc77aa463f0fb2f0ed36015bc75b2bd7cc35093f33b80d279ee49939c8b276dea862ee9b3660af62635a
7
- data.tar.gz: fa9c99fc53a11004a1525771ab9cf51f25693abf685f076b8bb14260674ab11f0af89a9ce3f39af329ee03ebc402c88b9520d151051d461cdba1db61ced7a746
6
+ metadata.gz: 9fa38dafe06b342668e826492f9219904833f1e0a91feb11754f8731187bf42106fcad467ec08c7b8d360fb6ddeb4c9cf68c1bd63ba69aa54a206126a79c1a79
7
+ data.tar.gz: 3c8de16fba64582e4d4fc9ec9bf121c924de7b3a736972c413f2e28b86c47446bd9154daed06658e5d303e6c3f4c1946e66b57aebad730fac5e3809766d130cf
@@ -54,6 +54,75 @@ if !defined? Colcon
54
54
  end
55
55
  end
56
56
 
57
+ #:stopdoc:
58
+
59
+ # We monkey-patch CLASP module's Flag and Option generator methods by
60
+ # added in a 'action' attribute (but only if it does not exist)
61
+ # and attaching the given block
62
+
63
+ class << CLASP
64
+
65
+ alias_method :Flag_old, :Flag
66
+ alias_method :Option_old, :Option
67
+
68
+ def Flag(name, options={}, &blk)
69
+
70
+ f = self.Flag_old(name, options)
71
+
72
+ # anticipate this functionality being added to CLASP
73
+ return f if f.respond_to? :action
74
+
75
+ class << f
76
+
77
+ attr_accessor :action
78
+ end
79
+
80
+ if blk
81
+
82
+ case blk.arity
83
+ when 0, 1, 2
84
+ else
85
+
86
+ warn "wrong arity for flag"
87
+ end
88
+
89
+ f.action = blk
90
+ end
91
+
92
+ f
93
+ end
94
+
95
+ def Option(name, options={}, &blk)
96
+
97
+ o = self.Option_old(name, options)
98
+
99
+ # anticipate this functionality being added to CLASP
100
+ return o if o.respond_to? :action
101
+
102
+ class << o
103
+
104
+ attr_accessor :action
105
+ end
106
+
107
+ if blk
108
+
109
+ case blk.arity
110
+ when 0, 1, 2
111
+ else
112
+
113
+ warn "wrong arity for option"
114
+ end
115
+
116
+ o.action = blk
117
+ end
118
+
119
+ o
120
+ end
121
+ end
122
+
123
+ #:startdoc:
124
+
125
+
57
126
  module LibCLImate
58
127
 
59
128
  # Class used to gather together the CLI specification, and execute it
@@ -189,15 +258,26 @@ class Climate
189
258
 
190
259
  selector = :unhandled
191
260
 
192
- ex = al.extras
261
+ # see if it has a :action attribute (which will have been
262
+ # monkey-patched to CLASP.Flag()
263
+
264
+ if al.respond_to?(:action) && !al.action.nil?
265
+
266
+ al.action.call(f, al)
267
+
268
+ selector = :handled
269
+ else
193
270
 
194
- case ex
195
- when ::Hash
196
- if ex.has_key? :handle
271
+ ex = al.extras
197
272
 
198
- ex[:handle].call(f, al)
273
+ case ex
274
+ when ::Hash
275
+ if ex.has_key? :handle
199
276
 
200
- selector = :handled
277
+ ex[:handle].call(f, al)
278
+
279
+ selector = :handled
280
+ end
201
281
  end
202
282
  end
203
283
 
@@ -229,15 +309,25 @@ class Climate
229
309
 
230
310
  selector = :unhandled
231
311
 
232
- ex = al.extras
312
+ # see if it has a :action attribute (which will have been
313
+ # monkey-patched to CLASP.Option()
314
+
315
+ if al.respond_to?(:action) && !al.action.nil?
316
+
317
+ al.action.call(o, al)
318
+
319
+ selector = :handled
320
+ else
321
+ ex = al.extras
233
322
 
234
- case ex
235
- when ::Hash
236
- if ex.has_key? :handle
323
+ case ex
324
+ when ::Hash
325
+ if ex.has_key? :handle
237
326
 
238
- ex[:handle].call(o, al)
327
+ ex[:handle].call(o, al)
239
328
 
240
- selector = :handled
329
+ selector = :handled
330
+ end
241
331
  end
242
332
  end
243
333
 
@@ -40,7 +40,7 @@
40
40
  module LibCLImate
41
41
 
42
42
  # Current version of the libCLImate.Ruby library
43
- VERSION = '0.2.4'
43
+ VERSION = '0.3.1'
44
44
 
45
45
  private
46
46
  VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # test Recls entries
4
+
5
+ $:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
6
+
7
+
8
+ require 'libclimate'
9
+
10
+ require 'xqsr3/extensions/test/unit'
11
+
12
+ require 'test/unit'
13
+
14
+ require 'stringio'
15
+
16
+ class Test_Climate_with_blocks < Test::Unit::TestCase
17
+
18
+ def test_flag_with_block
19
+
20
+ is_verbose = false
21
+
22
+ climate = LibCLImate::Climate.new do |climate|
23
+
24
+ climate.aliases << CLASP.Flag('--verbose') { is_verbose = true }
25
+ end
26
+
27
+ argv = %w{ --verbose }
28
+
29
+ climate.run argv
30
+
31
+ assert is_verbose, "block associated with flag '--verbose' was not executed"
32
+ end
33
+
34
+ def test_option_with_block
35
+
36
+ flavour = nil
37
+
38
+ climate = LibCLImate::Climate.new do |climate|
39
+
40
+ climate.aliases << CLASP.Option('--flavour') { |o| flavour = o.value }
41
+ end
42
+
43
+ argv = %w{ --flavour=blueberry }
44
+
45
+ climate.run argv
46
+
47
+ assert_equal 'blueberry', flavour
48
+ end
49
+ end
50
+
51
+ # ############################## end of file ############################# #
52
+
53
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libclimate-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Wilson
@@ -66,6 +66,7 @@ files:
66
66
  - test/scratch/blankzeroes.rb
67
67
  - test/unit/tc_minimal.rb
68
68
  - test/unit/tc_test_aliases.rb
69
+ - test/unit/tc_with_blocks.rb
69
70
  - test/unit/ts_all.rb
70
71
  homepage: http://www.libclimate.org/
71
72
  licenses: