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, #value

Instance Method Summary collapse

Methods inherited from Argument

#check_present, #consume_hash, #parse, register, #required?

Constructor Details

#initialize(name, block) ⇒ MultiArgument

Returns a new instance of MultiArgument.

Raises:

  • (TypeError)


457
458
459
460
461
# File 'lib/command-set/arguments.rb', line 457

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

Instance Method Details

#complete(prefix, subject) ⇒ Object



463
464
465
# File 'lib/command-set/arguments.rb', line 463

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

#consume(subject, arguments) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/command-set/arguments.rb', line 471

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



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

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)


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

def omittable?
  true
end

#validate(terms, subject) ⇒ Object



467
468
469
# File 'lib/command-set/arguments.rb', line 467

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