Class: Command::BaseInterpreter
- Inherits:
-
Object
- Object
- Command::BaseInterpreter
- Defined in:
- lib/command-set/interpreter.rb
Overview
This is the base interpreter class. By itself it’ll raise a bunch of NoMethodErrors.
Interpreters manage the Subject object(s) and CommandSet and process input.
Subclasses of BaseInterpreter (like TextInterpreter, for instance, must implement #cook_input(raw) that converts raw input (of whatever form is appropriate to the interpreter) into CommandSetup objects. Other methods can be overridden - especially consider #get_formatter, #prompt_user, and #assign_terms
Direct Known Subclasses
Instance Attribute Summary collapse
-
#command_set ⇒ Object
Returns the value of attribute command_set.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#out_io ⇒ Object
Returns the value of attribute out_io.
-
#subject ⇒ Object
Returns the value of attribute subject.
Instance Method Summary collapse
-
#behavior(hash) ⇒ Object
Any options that the interpreter might have can be set by passing a hash to behavior to be merged with the defaults.
-
#get_formatter ⇒ Object
Gets the final Results::Formatter object to output to.
- #get_subject ⇒ Object
-
#initialize ⇒ BaseInterpreter
constructor
A new instance of BaseInterpreter.
-
#next_command ⇒ Object
Gets the next command in the queue - related to DSL::Action#chain.
-
#pop_mode ⇒ Object
The compliment to #push_mode.
-
#prep_subject(subject) ⇒ Object
This method sets up the fields in the subject required by the interpreter.
-
#process_input(raw_input) ⇒ Object
Process a single unit of input from the user.
-
#prompt_user(message) ⇒ Object
Present
messageto the user, and get a response - usually yes or no. -
#push_mode(mode) ⇒ Object
Puts a CommandSet ahead of the current one for processing.
-
#subject_requirements ⇒ Object
If your interpreter needs extra fields in the subject, alter subject_requirements to return an array of those fields.
-
#subject_template ⇒ Object
Subject has an intentionally very tight interface (q.v.) the gist of which is that you can only assign to fields that commands require, and you can only access fields within a command that you declared that command required.
Constructor Details
#initialize ⇒ BaseInterpreter
Returns a new instance of BaseInterpreter.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/command-set/interpreter.rb', line 50 def initialize @command_set=nil @sub_modes = [] @behavior = { :screen_width => 76, :warn_no_undo => true } @out_io = $stdout @stop = false @subject = nil @logger = Logger.new($stderr) @logger.level=Logger::FATAL @undo_stack = UndoStack.new @commands_pending = [] @pause_decks = Hash.new {|h,k| h[k]=[]} end |
Instance Attribute Details
#command_set ⇒ Object
Returns the value of attribute command_set.
16 17 18 |
# File 'lib/command-set/interpreter.rb', line 16 def command_set @command_set end |
#logger ⇒ Object
Returns the value of attribute logger.
16 17 18 |
# File 'lib/command-set/interpreter.rb', line 16 def logger @logger end |
#out_io ⇒ Object
Returns the value of attribute out_io.
16 17 18 |
# File 'lib/command-set/interpreter.rb', line 16 def out_io @out_io end |
#subject ⇒ Object
Returns the value of attribute subject.
17 18 19 |
# File 'lib/command-set/interpreter.rb', line 17 def subject @subject end |
Instance Method Details
#behavior(hash) ⇒ Object
Any options that the interpreter might have can be set by passing a hash to behavior to be merged with the defaults
46 47 48 |
# File 'lib/command-set/interpreter.rb', line 46 def behavior(hash) @behavior.merge!(hash) end |
#get_formatter ⇒ Object
Gets the final Results::Formatter object to output to. Override this if you won’t be reporting output to the standard output.
172 173 174 |
# File 'lib/command-set/interpreter.rb', line 172 def get_formatter return Results::Formatter.new(::Command::raw_stdout) end |
#get_subject ⇒ Object
95 96 97 |
# File 'lib/command-set/interpreter.rb', line 95 def get_subject return Subject.new end |
#next_command ⇒ Object
Gets the next command in the queue - related to DSL::Action#chain. You’ll almost never want to override this method.
113 114 115 116 |
# File 'lib/command-set/interpreter.rb', line 113 def next_command setup = @commands_pending.shift return setup.command_instance(current_command_set, build_subject) end |
#pop_mode ⇒ Object
The compliment to #push_mode. Removes the most recent command set.
81 82 83 84 |
# File 'lib/command-set/interpreter.rb', line 81 def pop_mode @sub_modes.pop return nil end |
#prep_subject(subject) ⇒ Object
This method sets up the fields in the subject required by the interpreter.
101 102 103 104 105 106 107 108 109 |
# File 'lib/command-set/interpreter.rb', line 101 def prep_subject(subject) @command_set.add_requirements(subject) subject.required_fields(*subject_requirements()) subject.undo_stack = @undo_stack subject.interpreter_behavior = @behavior subject.chain_of_command = @commands_pending subject.pause_decks = @pause_decks return subject end |
#process_input(raw_input) ⇒ Object
Process a single unit of input from the user. Relies on cook input to convert raw_input into a CommandSetup
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/command-set/interpreter.rb', line 127 def process_input(raw_input) @commands_pending << cook_input(raw_input) presenter = Results::Presenter.new formatter = get_formatter presenter.register_formatter(formatter) collector = presenter.create_collector begin raise "Ack! No OutputStandin in place!" unless $stdout.respond_to?(:add_dispatcher) $stdout.add_dispatcher(collector) until @commands_pending.empty? cmd = next_command if ( @behavior[:warn_no_undo] and not cmd.undoable? ) confirm = prompt_user("\"#{raw_input}\" cannot be undone. Continue? ") if not ["yes", "y", "sure", "i suppose", "okay"].include? confirm.strip.downcase return end end begin execute(cmd, formatter, collector) rescue CommandException => ce ce.command = cmd raise rescue ResumeFrom => rf deck = rf.pause_deck @pause_decks[deck].push rf.setup raise unless ResumeFromOnlyThis === rf end end rescue ResumeFrom => rf @pause_decks[deck] += @commands_pending rescue CommandException => ce ce.raw_input = raw_input raise ensure $stdout.remove_dispatcher(collector) end presenter.done @commands_pending.clear end |
#prompt_user(message) ⇒ Object
Present message to the user, and get a response - usually yes or no. Non-interactive interpreters, or ones where that level of interaction is undesirable should not override this method, which returns “no”.
121 122 123 |
# File 'lib/command-set/interpreter.rb', line 121 def prompt_user() "no" end |
#push_mode(mode) ⇒ Object
Puts a CommandSet ahead of the current one for processing. Useful for command
modes, like Cisco's IOS with configure modes, et al.
71 72 73 74 75 76 77 78 |
# File 'lib/command-set/interpreter.rb', line 71 def push_mode(mode) unless CommandSet === mode raise RuntimeError, "Sub-modes must be CommandSets!" end @sub_modes.push(mode) return nil end |
#subject_requirements ⇒ Object
If your interpreter needs extra fields in the subject, alter subject_requirements to return an array of those fields.
90 91 92 93 |
# File 'lib/command-set/interpreter.rb', line 90 def subject_requirements return [:undo_stack, :interpreter_behavior, :chain_of_command, :pause_decks] end |
#subject_template ⇒ Object
Subject has an intentionally very tight interface (q.v.) the gist of which is that you can only assign to fields that commands require, and you can only access fields within a command that you declared that command required.
25 26 27 28 |
# File 'lib/command-set/interpreter.rb', line 25 def subject_template raise "CommandSet unset!" if @command_set.nil? return prep_subject(get_subject) end |