class RemoteRuby::Runner

Runner class is responsible for running a prepared Ruby code with given connection adapter, reading output and unmarshalling result and local variables values.

Attributes

adapter[R]
code[R]
err_stream[R]
out_stream[R]

Public Class Methods

new(code:, adapter:, out_stream: $stdout, err_stream: $stderr) click to toggle source
# File lib/remote_ruby/runner.rb, line 10
def initialize(code:, adapter:, out_stream: $stdout, err_stream: $stderr)
  @code = code
  @adapter = adapter
  @out_stream = out_stream
  @err_stream = err_stream
end

Public Instance Methods

run() click to toggle source
# File lib/remote_ruby/runner.rb, line 17
def run
  locals = nil

  adapter.open(code) do |stdout, stderr|
    out_thread = read_stream(stdout, out_stream, :green)
    err_thread = read_stream(stderr, err_stream, :red)
    [out_thread, err_thread].each(&:join)
    locals = out_thread[:locals]
  end

  { result: locals[:__return_val__], locals: locals }
end

Private Instance Methods

read_stream(read_from, write_to, color) click to toggle source
# File lib/remote_ruby/runner.rb, line 34
def read_stream(read_from, write_to, color)
  Thread.new do
    until read_from.eof?
      line = read_from.readline

      if line.start_with?('%%%MARSHAL')
        Thread.current[:locals] ||= unmarshal(read_from)
      else
        write_to.puts "#{adapter.connection_name.send(color)}>\t#{line}"
      end
    end
  end
end
unmarshal(stdout) click to toggle source
# File lib/remote_ruby/runner.rb, line 48
def unmarshal(stdout)
  unmarshaler = RemoteRuby::Unmarshaler.new(stdout)
  unmarshaler.unmarshal
end