Class: Command::QuickInterpreter

Inherits:
BaseInterpreter show all
Defined in:
lib/command-set/quick-interpreter.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, #subject

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseInterpreter

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

Constructor Details

#initializeQuickInterpreter

Returns a new instance of QuickInterpreter.



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

def initialize
  @formatter_factory = proc {Results::Formatter.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/quick-interpreter.rb', line 47

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

Instance Method Details

#cook_input(words) ⇒ Object

Passes the arguments to process_input directly to CommandSetup



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

def cook_input(words)
  CommandSetup.new(words)
end

#get_formatterObject

:nodoc:



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

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

#get_subjectObject

See PermissiveSubject



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

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/quick-interpreter.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
# File 'lib/command-set/quick-interpreter.rb', line 72

def process_input(*words)
  super(words)
end

#prompt_user(message) ⇒ Object

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



77
78
79
# File 'lib/command-set/quick-interpreter.rb', line 77

def prompt_user(message)
  "yes"
end