optbind 0.2.2 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 194d33bd932477ac7d450cc566424f849b34a198
4
- data.tar.gz: 66a5246dab6f09d49045990eef6599b7a4a38fb7
3
+ metadata.gz: bf2645df5139cfb5f2da9545d463f30b5e2e7fcd
4
+ data.tar.gz: 19b95d76da822aa643ba5ee62ad5c00e6ab86ef4
5
5
  SHA512:
6
- metadata.gz: 7a97b83b47f41d650ebfb865bf051df3309bcd6396d983034630a4969c331e4dc603ab4d9ff01bc9135f6dc67d32ebbfd77635b9b155828097bccf5d0ad4e505
7
- data.tar.gz: 6132c445ebe256c2e4669b449b69a2ddeae29306102ddbb1d6348f22971c8daabc253fbe4e7166401faf6d453297e33b105dde45184acc204fd647a296127cd4
6
+ metadata.gz: 33129020311fbd81302a02c87c18a11e8efbfe42f2ed66335b9cfa584dd2c023620918663088365e3147a47ecffef50c3abb40d11cfae29bd128753b2e5549d5
7
+ data.tar.gz: 65e76a032087b2590ffa173776c04f3e284ea707c8d1cc0cbdf246794a36cf27c5757dc37dd707bd05202f8a22e7309a6eee9db477924add3b23010e77b99b8e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 0.3.0
2
+
3
+ * Add support for access to assigned variables
4
+
5
+ * Fix default argument handler when encountering blank arguments
6
+
7
+ *Pavol Zbell*
8
+
9
+ ## 0.2.2
10
+
11
+ * Fix default option handler when encountering blank arguments
12
+
13
+ *Pavol Zbell*
14
+
1
15
  ## 0.2.1
2
16
 
3
17
  * Fix multiple argument parsing with arrays as default values
@@ -1,3 +1,3 @@
1
1
  module OptBind
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/optbind.rb CHANGED
@@ -74,7 +74,7 @@ class OptionBinder
74
74
  @parser.abort "missing argument: #{a}=" if !r || (r.respond_to?(:empty?) && r.empty?)
75
75
  end
76
76
 
77
- (handler || -> (x) { x }).call(r == nil ? default : r).tap { |x| @writer.call variable, x if bound }
77
+ handle! handler, r, bound, variable, default
78
78
  end
79
79
 
80
80
  (@bound_variables_with_defaults ||= {})[variable] = default if bound
@@ -106,10 +106,24 @@ class OptionBinder
106
106
  Hash[@bound_variables_with_defaults.keys.map { |v| [v, @reader.call(v)] }]
107
107
  end
108
108
 
109
+ def assigned_variables
110
+ return {} unless @assigned_variables_with_values
111
+ @assigned_variables_with_values.dup
112
+ end
113
+
109
114
  def default?(v)
110
115
  v = v.to_sym
111
- return nil unless (@bound_variables_with_defaults || {}).has_key? v
112
- @bound_variables_with_defaults[v] == @reader.call(v)
116
+ return nil unless bound? v
117
+ (@bound_variables_with_defaults[v] || {}) == @reader.call(v)
118
+ end
119
+
120
+ def bound?(v)
121
+ (@bound_variables_with_defaults || {}).has_key? v.to_sym
122
+ end
123
+
124
+ def assigned?(v)
125
+ return nil unless bound? v
126
+ (@assigned_variables_with_values || {}).has_key? v.to_sym
113
127
  end
114
128
 
115
129
  module Switch
@@ -200,7 +214,7 @@ class OptionBinder
200
214
  r = argv[0] ? argv.shift : default
201
215
  r = ([r].flatten + argv.shift(argv.size)) if a[:opts].include? :MULTIPLE
202
216
  @parser.abort 'missing arguments' if r.nil? && a[:opts].include?(:REQUIRED)
203
- (a[:handler] || -> (_) { r }).call(r == nil ? default : r).tap { |x| @writer.call a[:variable], x if a[:bound] }
217
+ handle! a[:handler], r, a[:bound], a[:variable], default
204
218
  return argv if a[:opts].include? :MULTIPLE
205
219
  end
206
220
  @parser.abort 'too many arguments' if argv[0]
@@ -209,6 +223,16 @@ class OptionBinder
209
223
 
210
224
  private :parse_args, :parse_args!
211
225
 
226
+ def handle!(handler, raw, bound, variable, default)
227
+ (handler || -> (r) { r }).call(raw == nil ? default : raw).tap do |x|
228
+ return x unless bound
229
+ @writer.call variable, x
230
+ (@assigned_variables_with_values ||= {})[variable] = x
231
+ end
232
+ end
233
+
234
+ private :handle!
235
+
212
236
  module Arguable
213
237
  def binder=(bind)
214
238
  unless @optbind = bind
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optbind
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavol Zbell