Class: Command::ProcArgument

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

Overview

Created with a two argument block, a proc_argument validates it’s input by passing it to the block. It also uses the block to validate.

As a result, the block should return a list of acceptable completions, given a prefix and the current subject.

Ideally, use proc_arguments to prototype new argument types before creating whole classes for them.

Direct Known Subclasses

NoValidateProcArgument

Instance Attribute Summary

Attributes inherited from Argument

#name, #value

Instance Method Summary collapse

Methods inherited from Argument

#check_present, #consume, #consume_hash, #match_terms, #omittable?, #parse, register, #required?

Constructor Details

#initialize(name, prok) ⇒ ProcArgument

Returns a new instance of ProcArgument.

Raises:

  • (TypeError)


377
378
379
380
381
# File 'lib/command-set/arguments.rb', line 377

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

Instance Method Details

#complete(prefix, subject) ⇒ Object



383
384
385
# File 'lib/command-set/arguments.rb', line 383

def complete(prefix, subject)
  return @process.call(prefix, subject)
end

#validate(term, subject) ⇒ Object



387
388
389
# File 'lib/command-set/arguments.rb', line 387

def validate(term, subject)
  return @process.call(term, subject).include?(term)
end