Class: NumRu::Misc::KeywordOpt

Inherits:
Object
  • Object
show all
Defined in:
lib/numru/misc/keywordopt.rb

Direct Known Subclasses

KeywordOptAutoHelp

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ KeywordOpt

Returns a new instance of KeywordOpt.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/numru/misc/keywordopt.rb', line 211

def initialize(*args)
	# USAGE:
	#    KeywordOpt.new([key,val,description],[key,val,description],..)
	#    where key is a String, and description can be omitted.
	@val=Hash.new
	@description=Hash.new
	@keys = []
	args.each{ |x|
	  case x
	  when Array
	    unless (x[0]=='help') && @keys.include?(x[0])
 #^only 'help' can overwrap in the arguments
 @keys.push(x[0])
 @val[x[0]] = x[1]
 @description[x[0]] = ( (x.length>=3) ? x[2] : '' )
	    end
	  when KeywordOpt
	    x.keys.each{|k|
 unless k=='help' && @keys.include?(k)
   #^only 'help' can overwrap in the arguments
		@keys.push(k)
		@val[k] = x  #.val[k]
		@description[k] = x.description[k]
 end
	    }
	    def @val.[](k)
 val = super(k)
 val.is_a?(KeywordOpt) ? val[k] : val
	    end
	    def @val.dup
 out = Hash.new
 each{|k,val| out[k] = (val.is_a?(KeywordOpt) ? val[k] : val)}
 out
	    end
	  else
	    raise ArgumentError, "invalid argument: #{x.inspect}"
	  end
	}
	@keys_sort = @keys.sort
	if @keys_sort.length != @keys_sort.uniq.length
	  raise ArgumentError, "keys are not unique"
	end
end

Instance Method Details

#[](k) ⇒ Object



352
353
354
355
356
357
358
# File 'lib/numru/misc/keywordopt.rb', line 352

def [](k)
	v = @val[k]
	if v.is_a?(KeywordOpt)
	  v = v.val[k]
	end
	v
end

#helpObject



345
346
347
348
349
350
# File 'lib/numru/misc/keywordopt.rb', line 345

def help
	 "  option name\tdefault value\t# description:\n" +
	 @keys.collect{|k| 
	   __line_feed("  #{k.inspect}\t#{@val[k].inspect}\t# #{@description[k]}", 66)
	 }.join("\n")
end

#interpret(hash) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/numru/misc/keywordopt.rb', line 255

def interpret(hash)
	return @val.dup if hash.nil?
	##
	len = @val.length
	im = 0
	out = @val.dup
	hash.keys.sort.each do |key|
	  rkey = /^#{key}/
	  loop do
	    if rkey =~ @keys_sort[im]
 if im<len-1 && rkey=~@keys_sort[im+1] &&
		 key != @keys_sort[im]   # not identical
		raise ArgumentError, "Ambiguous key specification '#{key}'." 
 end
 out[@keys_sort[im]]=hash[key]
 break
	    end
	    im += 1
	    if im==len
 raise ArgumentError, "'#{key}' does not match any of the keys."
	    end
	  end
	end
	out
end

#keysObject



360
361
362
# File 'lib/numru/misc/keywordopt.rb', line 360

def keys
	@keys.dup
end

#select_existent(hash_or_keys) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/numru/misc/keywordopt.rb', line 281

def select_existent(hash_or_keys)
	hash_or_keys = hash_or_keys.dup         # not to alter the original
	len = @val.length
	im = 0
	kys = ( Array === hash_or_keys ? hash_or_keys : hash_or_keys.keys )
	kys.sort.each do |key|
	  rkey = /^#{key}/
	  loop do
	    break if rkey =~ @keys_sort[im]
	    im += 1
	    if im==len
 hash_or_keys.delete(key)
 im = 0           # rewind
        break
	    end
	  end
	end
	hash_or_keys
end

#set(hash) ⇒ Object

Raises:

  • (ArgumentError)


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/numru/misc/keywordopt.rb', line 301

def set(hash)
	raise ArgumentError, "not a hash" if !hash.is_a?(Hash)
	##
	replaced = Hash.new
	len = @val.length
	im = 0
	hash.keys.sort.each do |key|
	  rkey = /^#{key}/
	  loop do
	    if rkey =~ @keys_sort[im]
 if im<len-1 && rkey=~@keys_sort[im+1]
		raise "Ambiguous key specification '#{key}'." 
 end
 replaced[@keys_sort[im]] = @val[@keys_sort[im]]
 @val[@keys_sort[im]]=hash[key]
 break
	    end
	    im += 1
	    raise "'#{key}' does not match any of the keys." if im==len
	  end
	end
	replaced
end