Class: RemoteRuby::RemoteContext

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_ruby/remote_context.rb

Overview

This class is inlined to the remote script and used to collect errors and other information about the remote run. It is serialized and sent back to the client.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ RemoteContext



12
13
14
15
16
# File 'lib/remote_ruby/remote_context.rb', line 12

def initialize(filename)
  @file_name = filename
  @has_error = false
  @locals = {}
end

Instance Attribute Details

#error_backtraceObject (readonly)

Returns the value of attribute error_backtrace.



10
11
12
# File 'lib/remote_ruby/remote_context.rb', line 10

def error_backtrace
  @error_backtrace
end

#error_classObject (readonly)

Returns the value of attribute error_class.



10
11
12
# File 'lib/remote_ruby/remote_context.rb', line 10

def error_class
  @error_class
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



10
11
12
# File 'lib/remote_ruby/remote_context.rb', line 10

def error_message
  @error_message
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



10
11
12
# File 'lib/remote_ruby/remote_context.rb', line 10

def file_name
  @file_name
end

#has_errorObject (readonly)

Returns the value of attribute has_error.



10
11
12
# File 'lib/remote_ruby/remote_context.rb', line 10

def has_error
  @has_error
end

#localsObject (readonly)

Returns the value of attribute locals.



10
11
12
# File 'lib/remote_ruby/remote_context.rb', line 10

def locals
  @locals
end

#resultObject (readonly)

Returns the value of attribute result.



10
11
12
# File 'lib/remote_ruby/remote_context.rb', line 10

def result
  @result
end

Instance Method Details

#dumpObject



41
42
43
# File 'lib/remote_ruby/remote_context.rb', line 41

def dump
  Marshal.dump(self)
end

#error?Boolean



18
19
20
# File 'lib/remote_ruby/remote_context.rb', line 18

def error?
  @has_error
end

#execute(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/remote_ruby/remote_context.rb', line 29

def execute(&block)
  @result = begin
    block.call
  rescue StandardError => e
    handle_error(e)
  ensure
    locals.each_key do |name|
      locals[name] = block.binding.local_variable_get(name)
    end
  end
end

#handle_error(err) ⇒ Object



22
23
24
25
26
27
# File 'lib/remote_ruby/remote_context.rb', line 22

def handle_error(err)
  @error_class = err.class.to_s
  @error_message = err.message
  @error_backtrace = err.backtrace
  @has_error = true
end

#unmarshal(name, data) ⇒ Object



45
46
47
48
49
50
# File 'lib/remote_ruby/remote_context.rb', line 45

def unmarshal(name, data)
  locals[name] = Marshal.load(Base64.strict_decode64(data)) # rubocop:disable Security/MarshalLoad
rescue ArgumentError
  warn("Warning: could not resolve type for '#{name}' variable")
  nil
end