Class: ReplRunner::MultiRepl
- Inherits:
-
Object
- Object
- ReplRunner::MultiRepl
- Defined in:
- lib/repl_runner/multi_repl.rb
Overview
Takes in a command, to start a REPL session with PTY builds up a list of commands we want to run in our REPL executes them all at a time, reads in the result and returns results back to blocks
Defined Under Namespace
Classes: UnexpectedExit
Constant Summary collapse
- DEFAULT_STARTUP_TIMEOUT =
60- DEFAULT_RETURN_CHAR =
"\n"
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #close ⇒ Object
- #dead? ⇒ Boolean
- #execute ⇒ Object
-
#initialize(command, options = {}) ⇒ MultiRepl
constructor
A new instance of MultiRepl.
- #parse_results ⇒ Object
- #pty ⇒ Object
- #read ⇒ Object
- #run(command, &block) ⇒ Object
- #write(command) ⇒ Object
Constructor Details
#initialize(command, options = {}) ⇒ MultiRepl
Returns a new instance of MultiRepl.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/repl_runner/multi_repl.rb', line 15 def initialize(command, = {}) @command_parser = [:command_parser] || MultiCommandParser @return_char = [:return_char] || DEFAULT_RETURN_CHAR @startup_timeout = [:startup_timeout] || DEFAULT_STARTUP_TIMEOUT @terminate_command = [:terminate_command] or raise "must set default `terminate_command`" @stync_stdout = [:sync_stdout] @command = command @commands = [] @jobs = [] end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
13 14 15 |
# File 'lib/repl_runner/multi_repl.rb', line 13 def command @command end |
Instance Method Details
#alive? ⇒ Boolean
40 41 42 |
# File 'lib/repl_runner/multi_repl.rb', line 40 def alive? !!::Process.kill(0, pty.pid) rescue false # kill zero will return the status of the proceess without killing it end |
#close ⇒ Object
57 58 59 |
# File 'lib/repl_runner/multi_repl.rb', line 57 def close pty.close end |
#dead? ⇒ Boolean
44 45 46 |
# File 'lib/repl_runner/multi_repl.rb', line 44 def dead? !alive? end |
#execute ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/repl_runner/multi_repl.rb', line 61 def execute write(@sync_stdout) if @sync_stdout @commands.each do |command| write(command) end write(@terminate_command) output_array = parse_results @jobs.each_with_index do |job, index| job.call(output_array[index]) end rescue NoResultsError => e raise e, "Booting up REPL with command: #{command.inspect} \n#{e.message}" end |
#parse_results ⇒ Object
53 54 55 |
# File 'lib/repl_runner/multi_repl.rb', line 53 def parse_results @parsed_results ||= @command_parser.new(@commands, @terminate_command).parse(read) end |
#pty ⇒ Object
27 28 29 |
# File 'lib/repl_runner/multi_repl.rb', line 27 def pty @pty ||= PtyParty.new(command) end |
#read ⇒ Object
48 49 50 51 |
# File 'lib/repl_runner/multi_repl.rb', line 48 def read raise UnexpectedExit, "Repl: '#{@command}' exited unexpectedly" if dead? @output = pty.read(@startup_timeout) end |
#run(command, &block) ⇒ Object
31 32 33 34 |
# File 'lib/repl_runner/multi_repl.rb', line 31 def run(command, &block) @commands << command @jobs << (block || Proc.new {|result| }) end |
#write(command) ⇒ Object
36 37 38 |
# File 'lib/repl_runner/multi_repl.rb', line 36 def write(command) pty.write("#{command}#{@return_char}") end |