Class: Command::MultiArgument

Inherits:
Argument
  • Object
show all
Defined in:
lib/command-set/arguments.rb

Overview

Consumes multiple words as dictated by the block used to create the argument. A little complex, but powerful. For use, see StandardCommands::Set

Instance Attribute Summary

Attributes inherited from Argument

#name

Instance Method Summary collapse

Methods inherited from Argument

#basis, #check_present, #consume_hash, #names, #parse, register, #required?, #subject_requirements

Constructor Details

#initialize(name, block) ⇒ MultiArgument

Returns a new instance of MultiArgument.

Raises:

  • (TypeError)


474
475
476
477
478
# File 'lib/command-set/arguments.rb', line 474

def initialize(name, block)
  raise TypeError, "Block arity is #{prok.arity}, not 2" unless block.arity == 2
  super(name,nil)
  @process = proc &block
end

Instance Method Details

#complete(terms, prefix, subject) ⇒ Object



480
481
482
# File 'lib/command-set/arguments.rb', line 480

def complete(terms, prefix, subject)
  return @process[[*prefix], subject]
end

#consume(subject, arguments) ⇒ Object



488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/command-set/arguments.rb', line 488

def consume(subject, arguments)
  value = []
  until arguments.empty? do
	trying = arguments.shift
	if(validate(value + [trying], subject))
	  value << trying
	else
	  arguments.unshift(trying)
	  break
	end
  end
  return {@name => value}
end

#match_terms(subject, terms, arguments) ⇒ Object



506
507
508
509
510
511
512
513
514
# File 'lib/command-set/arguments.rb', line 506

def match_terms(subject, terms, arguments)
  validated = validate(terms.first, subject)
  if(validated)
	terms.shift
  else
	arguments.shift
  end
  return true
end

#omittable?Boolean

Returns:

  • (Boolean)


502
503
504
# File 'lib/command-set/arguments.rb', line 502

def omittable?
  true
end

#validate(terms, subject) ⇒ Object



484
485
486
# File 'lib/command-set/arguments.rb', line 484

def validate(terms, subject)
  return (not @process[[*terms.dup], subject].empty?)
end