Class: Vivarium::Box

Inherits:
Module
  • Object
show all
Defined in:
lib/vivarium/box.rb

Overview

Box provides an isolated execution context where method calls are automatically traced through Vivarium’s observation system.

Usage:

box = Vivarium::Box.new
box.eval('class MyClass; def foo; "result"; end; end')
result = box::MyClass.new.foo  # automatically traced if Vivarium.observe is active

Constant Summary collapse

DEFAULT_FILTER =
Vivarium::DEFAULT_FILTER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket_path: Vivarium.socket_path, dest: $stdout, filter: DEFAULT_FILTER) ⇒ Box

Returns a new instance of Box.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vivarium/box.rb', line 17

def initialize(socket_path: Vivarium.socket_path, dest: $stdout, filter: DEFAULT_FILTER)
  super()
  @inner_box = Ruby::Box.new
  @socket_path = socket_path
  @dest = dest
  @filter = filter
  @session = nil

  @tracing_level = [0]
  # Set up TracePoint to automatically trace method calls within this box
  @tracer = TracePoint.new(:call, :return) do |tp|
    handle_trace_event(tp, @tracing_level, @inner_box)
  end
end

Instance Attribute Details

#inner_boxObject (readonly)

Returns the value of attribute inner_box.



31
32
33
# File 'lib/vivarium/box.rb', line 31

def inner_box
  @inner_box
end

#tracerObject (readonly)

Returns the value of attribute tracer.



31
32
33
# File 'lib/vivarium/box.rb', line 31

def tracer
  @tracer
end

Instance Method Details

#const_missing(name) ⇒ Object

Intercept constant access to resolve from the box’s evaluated context



73
74
75
76
77
# File 'lib/vivarium/box.rb', line 73

def const_missing(name)
  @inner_box.const_get(name)
rescue NameError => e
  raise NameError, "#{name} not found in box: #{e.message}"
end

#done_load!Object



79
80
81
# File 'lib/vivarium/box.rb', line 79

def done_load!
  @tracer.enable
end

#eval(code) ⇒ Object

Evaluate code within the box context



34
35
36
37
38
39
40
# File 'lib/vivarium/box.rb', line 34

def eval(code)
  result = nil
  Vivarium.observe(filter: @filter) do
    result = @inner_box.eval(code)
  end
  result
end

#load(path, wrap = false) ⇒ Object

Load a file within the box context (executed every time, unlike require) Automatically traced if Vivarium.observe is active



64
65
66
67
68
69
70
# File 'lib/vivarium/box.rb', line 64

def load(path, wrap = false)
  result = nil
  Vivarium.observe(filter: @filter) do
    result = @inner_box.load(path, wrap)
  end
  result
end

#require(path) ⇒ Object

Require a file within the box context Automatically traced if Vivarium.observe is active



44
45
46
47
48
49
50
# File 'lib/vivarium/box.rb', line 44

def require(path)
  result = nil
  Vivarium.observe(filter: @filter) do
    result = @inner_box.require(path)
  end
  result
end

#require_relative(path) ⇒ Object

Require a file relative to the current file within the box context Automatically traced if Vivarium.observe is active



54
55
56
57
58
59
60
# File 'lib/vivarium/box.rb', line 54

def require_relative(path)
  result = nil
  Vivarium.observe(filter: @filter) do
    result = @inner_box.require_relative(path)
  end
  result
end