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

Instance Method Summary collapse

Methods inherited from Argument

#basis, #check_present, #consume, #consume_hash, #match_terms, #names, #omittable?, #parse, register, #required?, #subject_requirements

Constructor Details

#initialize(name, prok) ⇒ ProcArgument

Returns a new instance of ProcArgument.

Raises:

  • (TypeError)


395
396
397
398
399
# File 'lib/command-set/arguments.rb', line 395

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

Instance Method Details

#complete(terms, prefix, subject) ⇒ Object



401
402
403
# File 'lib/command-set/arguments.rb', line 401

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

#validate(term, subject) ⇒ Object



405
406
407
# File 'lib/command-set/arguments.rb', line 405

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