Class: Command::QuickInterpreter

Inherits:
BaseInterpreter show all
Defined in:
lib/command-set/interpreter/quick.rb

Overview

This class exists mostly to make spec and unit test writing easier. Because Commands need so much care and feeding on the back end, it can be troublesome to write specs on the directly. QuickInterpreter is designed for programmatic access, and easy setup.

Example usage:

@interpreter = QuickInterpreter::define_interpreter do #unique to QI
  command :test do
    subject.a_field << "one"
  end
end

@subject = @interpreter.subject_template
@subject.a_field = []
@interpreter.subject = @subject

@interpreter.process_input(:test, "args")
@subject.a_field.length  # => 1; note that normally you need to
       #+get_image+ to access fields of the subject

Instance Attribute Summary

Attributes inherited from BaseInterpreter

#command_set, #logger, #out_io, #sub_modes, #subject

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseInterpreter

#behavior, #fill_subject, #get_formatters, #next_command, #pop_mode, #prep_subject, #push_mode, #subject_requirements

Constructor Details

#initializeQuickInterpreter

Returns a new instance of QuickInterpreter.



56
57
58
59
# File 'lib/command-set/interpreter/quick.rb', line 56

def initialize
  @formatter_factory = proc {Results::TextFormatter.new(::Command::raw_stdout)}
  super
end

Class Method Details

.define_interpreter(&block) ⇒ Object Also known as: define_commands

You can use this method to create a new interpreter with a command set. The block is passed to a new CommandSet, so you can use DSL::CommandSetDefinition there.



47
48
49
50
51
# File 'lib/command-set/interpreter/quick.rb', line 47

def define_interpreter(&block)
  interpreter = new
  interpreter.command_set = CommandSet::define_commands(&block)
  return interpreter
end

Instance Method Details

#complete_input(terms, word) ⇒ Object



90
91
92
# File 'lib/command-set/interpreter/quick.rb', line 90

def complete_input(terms, word)
  return current_command_set.completion_list(terms, word, build_subject)
end

#cook_input(words) ⇒ Object

Passes the arguments to process_input directly to CommandSetup



86
87
88
# File 'lib/command-set/interpreter/quick.rb', line 86

def cook_input(words)
  current_command_set.process_terms(words, subject)
end

#get_formatterObject

:nodoc:



67
68
69
# File 'lib/command-set/interpreter/quick.rb', line 67

def get_formatter #:nodoc:
  return @formatter_factory.call
end

#get_subjectObject

See PermissiveSubject



95
96
97
# File 'lib/command-set/interpreter/quick.rb', line 95

def get_subject
  return PermissiveSubject.new
end

#make_formatter(&block) ⇒ Object

Saves the block passed to create formatters with. Cleaner a singleton get_formatter definition.



63
64
65
# File 'lib/command-set/interpreter/quick.rb', line 63

def make_formatter(&block)
  @formatter_factory = proc &block
end

#process_input(*words) ⇒ Object

Accepts it’s input as multiple arguments for convenience



72
73
74
75
76
77
78
# File 'lib/command-set/interpreter/quick.rb', line 72

def process_input(*words)
  if words.length == 1 and Array === words.first
    super words.first
  else
    super(words)
  end
end

#prompt_user(message) ⇒ Object

Always returns “yes” so that undo warnings can be ignored.



81
82
83
# File 'lib/command-set/interpreter/quick.rb', line 81

def prompt_user(message)
  "yes"
end