ridl 2.8.1 → 2.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ridl/options.rb CHANGED
@@ -14,13 +14,11 @@ require 'ostruct'
14
14
  require 'json'
15
15
 
16
16
  module IDL
17
-
18
17
  RIDLRC = '.ridlrc'
19
18
  RIDLRC_GLOBAL = File.expand_path(File.join(ENV['HOME'] || ENV['HOMEPATH'] || '~', RIDLRC))
20
19
 
21
20
  class Options < OpenStruct
22
-
23
- def initialize(hash=nil, marked=nil)
21
+ def initialize(hash = nil, marked = nil)
24
22
  super(hash)
25
23
  @marked = marked
26
24
  end
@@ -35,7 +33,7 @@ module IDL
35
33
  end
36
34
 
37
35
  def copy!(from, *keys)
38
- keys.flatten.each {|k| self[k] = from[k] }
36
+ keys.flatten.each { |k| self[k] = from[k] }
39
37
  self
40
38
  end
41
39
 
@@ -67,7 +65,6 @@ module IDL
67
65
  self.class.new(_dup_elem(@marked || @table), @marked)
68
66
  end
69
67
 
70
-
71
68
  def load(rcpath)
72
69
  IDL.log(3, "Loading #{RIDLRC} from #{rcpath}")
73
70
  _cfg = JSON.parse(IO.read(rcpath))
@@ -94,7 +91,7 @@ module IDL
94
91
  protected
95
92
 
96
93
  def _merge(to, from, *keys)
97
- keys = keys.flatten.collect {|k| k.to_sym}
94
+ keys = keys.flatten.collect { |k| k.to_sym }
98
95
  keys = from.keys if keys.empty?
99
96
  keys.each do |k|
100
97
  if from.has_key?(k)
@@ -122,9 +119,10 @@ module IDL
122
119
  def _dup_elem(v)
123
120
  case v
124
121
  when Array
125
- v.collect {|e| _dup_elem(e) }
122
+ v.collect { |e| _dup_elem(e) }
126
123
  when Hash
127
- v.inject({}) {|h, (k, e)| h[k] = _dup_elem(e); h }
124
+ v.inject({}) { |h, (k, e)| h[k] = _dup_elem(e)
125
+ h }
128
126
  when OpenStruct
129
127
  v.class.new(_dup_elem(v.__send__(:table)))
130
128
  else
@@ -136,12 +134,12 @@ module IDL
136
134
 
137
135
  def self.load_config(opt)
138
136
  # first collect config from known (standard and configured) locations
139
- _rc_paths = [ RIDLRC_GLOBAL ]
137
+ _rc_paths = [RIDLRC_GLOBAL]
140
138
  _loaded_rc_paths = []
141
139
  (ENV['RIDLRC'] || '').split(/:|;/).each do |p|
142
140
  _rc_paths << p unless _rc_paths.include?(p)
143
141
  end
144
- _rc_paths.collect {|path| File.expand_path(path) }.each do |rcp|
142
+ _rc_paths.collect { |path| File.expand_path(path) }.each do |rcp|
145
143
  IDL.log(3, "Testing rc path #{rcp}")
146
144
  if File.readable?(rcp) && !_loaded_rc_paths.include?(rcp)
147
145
  opt.load(rcp)
@@ -163,6 +161,7 @@ module IDL
163
161
  IDL.log(3, "Ignoring #{File.readable?(_rcp) ? 'already loaded' : 'inaccessible'} rc path #{_rcp}")
164
162
  end
165
163
  break if /\A(.:(\\|\/)|\.|\/)\Z/ =~ _cwd
164
+
166
165
  _cwd = File.dirname(_cwd)
167
166
  end while true
168
167
  # now load them in reverse order
@@ -172,5 +171,4 @@ module IDL
172
171
  end
173
172
  end
174
173
  end
175
-
176
174
  end
@@ -47,13 +47,9 @@ end
47
47
 
48
48
  module IDL
49
49
  class OptionList
50
-
51
50
  class Option
52
-
53
51
  class Group
54
-
55
52
  class ParamSet
56
-
57
53
  class Configurator
58
54
  def initialize(set)
59
55
  @set = set
@@ -70,11 +66,12 @@ module IDL
70
66
  end
71
67
 
72
68
  def without(*params)
73
- params.each {|p| @set.params.delete(p.to_sym) }
69
+ params.each { |p| @set.params.delete(p.to_sym) }
74
70
  end
75
71
  end
76
72
 
77
73
  attr_reader :params
74
+
78
75
  def initialize(options)
79
76
  @description = Array === options[:description] ? options[:description] : (options[:description] || '').split('\n')
80
77
  @all_params = options[:all_params] == true
@@ -108,7 +105,7 @@ module IDL
108
105
  end
109
106
 
110
107
  def description
111
- @params.values.inject(@description) {|list, vopt| list.concat(vopt[:description] || []) }
108
+ @params.values.inject(@description) { |list, vopt| list.concat(vopt[:description] || []) }
112
109
  end
113
110
 
114
111
  def define_params(spec = {})
@@ -116,7 +113,7 @@ module IDL
116
113
  when String, Hash
117
114
  define_param(spec)
118
115
  when Array
119
- spec.each {|p| define_param(p) }
116
+ spec.each { |p| define_param(p) }
120
117
  end
121
118
  end
122
119
 
@@ -124,6 +121,7 @@ module IDL
124
121
 
125
122
  def to_key(param)
126
123
  return param if Symbol === param
124
+
127
125
  # convert empty strings to single space strings before symbolizing
128
126
  String === param ? (param.empty? ? ' ' : param).to_sym : nil
129
127
  end
@@ -132,18 +130,18 @@ module IDL
132
130
  case spec
133
131
  when String
134
132
  key = to_key(spec)
135
- @params[key] = { :option_name => key }
133
+ @params[key] = { option_name: key }
136
134
  when Hash
137
135
  spec.each do |k, v|
138
136
  @params[to_key(k)] = (if Hash === v
139
137
  {
140
- :option_name => to_key(v[:option_name] || k),
141
- :option_type => v[:type],
142
- :option_value => v.has_key?(:value) ? v[:value] : true,
143
- :description => Array === v[:description] ? v[:description] : (v[:description] || '').split('\n')
138
+ option_name: to_key(v[:option_name] || k),
139
+ option_type: v[:type],
140
+ option_value: v.has_key?(:value) ? v[:value] : true,
141
+ description: Array === v[:description] ? v[:description] : (v[:description] || '').split('\n')
144
142
  }
145
143
  else
146
- { :option_name => to_key(v) }
144
+ { option_name: to_key(v) }
147
145
  end)
148
146
  end
149
147
  end
@@ -164,6 +162,7 @@ module IDL
164
162
  def define_param_set(id, options = {}, &block)
165
163
  id = id.to_sym
166
164
  raise "option parameter set [#{id}] already exists" if @group.sets.has_key?(id)
165
+
167
166
  @group.sets[id] = ParamSet.new(options)
168
167
  block.call(ParamSet::Configurator.new(@group.sets[id])) if block_given?
169
168
  end
@@ -179,7 +178,7 @@ module IDL
179
178
  alias :modify_params :modify_param_set
180
179
  alias :with_params :modify_param_set
181
180
 
182
- def define_param(id, options={}, &block)
181
+ def define_param(id, options = {}, &block)
183
182
  define_param_set("#{id}_set", options) do |pscfg|
184
183
  pscfg.with(id)
185
184
  pscfg.on_exec(&block)
@@ -193,21 +192,21 @@ module IDL
193
192
  end
194
193
  alias :without_set :without_param
195
194
  alias :without_params :without_param
196
-
197
195
  end # Configurator
198
196
 
199
197
  attr_reader :sets
198
+
200
199
  def initialize(id, options)
201
200
  @test = options[:test] || true
202
201
  @description = Array === options[:description] ? options[:description] : (options[:description] || '').split('\n')
203
202
  @sets = {}
204
203
  if options[:params] && Hash === options[:params]
205
- @sets[id] = ParamSet.new(:params => options[:params])
204
+ @sets[id] = ParamSet.new(params: options[:params])
206
205
  end
207
206
  end
208
207
 
209
208
  def description
210
- @sets.values.inject(@description.dup) {|desc, a| desc.concat(a.description) }
209
+ @sets.values.inject(@description.dup) { |desc, a| desc.concat(a.description) }
211
210
  end
212
211
 
213
212
  def run(arg, options)
@@ -215,6 +214,7 @@ module IDL
215
214
  if self.respond_to?(:_prepare, true)
216
215
  result = _prepare(arg, options)
217
216
  return false unless result && !result.empty?
217
+
218
218
  arg = result.shift
219
219
  ext_args = result
220
220
  else
@@ -232,7 +232,7 @@ module IDL
232
232
  private
233
233
 
234
234
  def handle_sets(param, options, *ext_args)
235
- @sets.values.inject(false) {|f, s| s.run(param, options, *ext_args) || f }
235
+ @sets.values.inject(false) { |f, s| s.run(param, options, *ext_args) || f }
236
236
  end
237
237
  end # Group
238
238
 
@@ -244,6 +244,7 @@ module IDL
244
244
  def define_group(id, options = {}, &block)
245
245
  id = id.to_sym
246
246
  raise "option group [#{id}] already exists" if @option.groups.has_key?(id)
247
+
247
248
  @option.groups[id] = Group.new(id, options)
248
249
  block.call(Group::Configurator.new(@option.groups[id])) if block_given?
249
250
  end
@@ -254,7 +255,7 @@ module IDL
254
255
  parms = options[:params] ? options.delete(:params) : options.delete(:param)
255
256
  @option.groups[id] ||= Group.new(id, options)
256
257
  grpcfg = Group::Configurator.new(@option.groups[id])
257
- grpcfg.modify_param_set(id, :params => parms) if parms
258
+ grpcfg.modify_param_set(id, params: parms) if parms
258
259
  block.call(grpcfg) if block_given?
259
260
  end
260
261
  alias :with_group :modify_group
@@ -264,23 +265,23 @@ module IDL
264
265
  end
265
266
 
266
267
  def define_param_set(id, options = {}, &block)
267
- modify_group :default, {:test => true} do |grpcfg|
268
+ modify_group :default, {test: true} do |grpcfg|
268
269
  grpcfg.define_param_set(id, options, &block)
269
270
  end
270
271
  end
271
272
  alias :for_set :define_param_set
272
273
  alias :for_params :define_param_set
273
274
 
274
- def on_exec(options={}, &block)
275
- modify_group :default, {:test => true} do |grpcfg|
276
- grpcfg.modify_param_set(:default, options.merge({:all_params => true})) do |pscfg|
275
+ def on_exec(options = {}, &block)
276
+ modify_group :default, {test: true} do |grpcfg|
277
+ grpcfg.modify_param_set(:default, options.merge({all_params: true})) do |pscfg|
277
278
  pscfg.on_exec(&block)
278
279
  end
279
280
  end
280
281
  end
281
282
 
282
- def define_param(id, options={}, &block)
283
- modify_group :default, {:test => true} do |grpcfg|
283
+ def define_param(id, options = {}, &block)
284
+ modify_group :default, {test: true} do |grpcfg|
284
285
  grpcfg.define_param_set("#{id}_set", options) do |pscfg|
285
286
  pscfg.with(id)
286
287
  pscfg.on_exec(&block)
@@ -300,10 +301,8 @@ module IDL
300
301
  alias :without_params :without_param
301
302
  end
302
303
 
303
- attr_reader :switch
304
- attr_reader :type
305
- attr_reader :separator
306
- attr_reader :groups
304
+ attr_reader :switch, :type, :separator, :groups
305
+
307
306
  def initialize(switch, options)
308
307
  @switch = switch
309
308
  @type = options[:type] || TrueClass
@@ -313,23 +312,24 @@ module IDL
313
312
  end
314
313
 
315
314
  def description(indent = "")
316
- @groups.values.inject(@description.dup) {|desc, h| desc.concat(h.description.collect {|desc| "\r#{indent} #{desc}"}) }
315
+ @groups.values.inject(@description.dup) { |desc, h| desc.concat(h.description.collect { |desc| "\r#{indent} #{desc}" }) }
317
316
  end
318
317
 
319
318
  def run(arg, options)
320
- unless @groups.values.inject(false) {|f, h| h.run(arg, options) || f }
319
+ unless @groups.values.inject(false) { |f, h| h.run(arg, options) || f }
321
320
  raise ArgumentError, "unknown option [#{arg}] for switch '#{@switch}'"
322
321
  end
323
322
  end
324
323
  end # Option
325
324
 
326
- def initialize()
325
+ def initialize
327
326
  @options = {}
328
327
  end
329
328
 
330
329
  def define_switch(switch, options = {}, &block)
331
330
  switch = switch.to_s
332
331
  raise "switch types mismatch" if @options.has_key?(switch) && options[:type] && options[:type] != @options[switch].type
332
+
333
333
  @options[switch] ||= Option.new(switch, options)
334
334
  block.call(Option::Configurator.new(@options[switch])) if block_given?
335
335
  end
@@ -351,7 +351,5 @@ module IDL
351
351
  optp.separator '' if op.separator
352
352
  end
353
353
  end
354
-
355
354
  end # OptionList
356
-
357
355
  end # IDL