class RemoteRuby::EvalAdapter

An adapter to expecute Ruby code in the current process in an isolated scope

Attributes

async[R]
working_dir[R]

Public Class Methods

new(working_dir: Dir.pwd, async: false) click to toggle source
# File lib/remote_ruby/connection_adapter/eval_adapter.rb, line 7
def initialize(working_dir: Dir.pwd, async: false)
  @async = async
  @working_dir = working_dir
end

Public Instance Methods

connection_name() click to toggle source
# File lib/remote_ruby/connection_adapter/eval_adapter.rb, line 12
def connection_name
  ''
end
open(code) { |out, err| ... } click to toggle source
# File lib/remote_ruby/connection_adapter/eval_adapter.rb, line 16
def open(code)
  if async
    run_async(code) do |out, err|
      yield out, err
    end
  else
    run_sync(code) do |out, err|
      yield out, err
    end
  end
end

Private Instance Methods

run_async(code) { |out_read, err_read| ... } click to toggle source
# File lib/remote_ruby/connection_adapter/eval_adapter.rb, line 52
def run_async(code)
  with_pipes do |out_read, out_write, err_read, err_write|
    Thread.new do
      with_tmp_streams(out_write, err_write) do
        run_code(code)
      end

      out_write.close
      err_write.close
    end

    yield out_read, err_read
  end
end
run_code(code) click to toggle source
# File lib/remote_ruby/connection_adapter/eval_adapter.rb, line 30
def run_code(code)
  binder = Object.new

  Dir.chdir(working_dir) do
    binder.instance_eval(code)
  end
end
run_sync(code) { |tmp_stdout, tmp_stderr| ... } click to toggle source
# File lib/remote_ruby/connection_adapter/eval_adapter.rb, line 38
def run_sync(code)
  with_stringio do |tmp_stdout, tmp_stderr|
    with_tmp_streams(tmp_stdout, tmp_stderr) do
      run_code(code)
    end

    tmp_stdout.close_write
    tmp_stderr.close_write
    tmp_stdout.rewind
    tmp_stderr.rewind
    yield tmp_stdout, tmp_stderr
  end
end
with_pipes() { |out_read, out_write, err_read, err_write| ... } click to toggle source
# File lib/remote_ruby/connection_adapter/eval_adapter.rb, line 67
def with_pipes
  out_read, out_write = IO.pipe
  err_read, err_write = IO.pipe
  yield out_read, out_write, err_read, err_write
ensure
  out_read.close
  err_read.close
end
with_stringio() { |out, err| ... } click to toggle source
# File lib/remote_ruby/connection_adapter/eval_adapter.rb, line 76
def with_stringio
  out = StringIO.new
  err = StringIO.new

  yield out, err
ensure
  out.close
  err.close
end
with_tmp_streams(out, err) { || ... } click to toggle source
# File lib/remote_ruby/connection_adapter/eval_adapter.rb, line 86
def with_tmp_streams(out, err)
  old_stdout = $stdout
  old_stderr = $stderr
  $stdout = out
  $stderr = err
  yield
ensure
  $stdout = old_stdout
  $stderr = old_stderr
end