Class: RobotArmy::EvalBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/robot-army/eval_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(command) ⇒ Object



2
3
4
# File 'lib/robot-army/eval_builder.rb', line 2

def self.build(command)
  new.build(command)
end

Instance Method Details

#build(command) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/robot-army/eval_builder.rb', line 6

def build(command)
  proc, procargs, context, dependencies =
    command.proc, command.args, command.context, command.dependencies

  options = {}
  proxies = {context.hash => context}

  # fix stack traces
  file, line = eval('[__FILE__, __LINE__]', proc.binding)

  # include local variables
  local_variables = eval('local_variables', proc.binding)
  locals, lproxies = dump_values(local_variables) { |name,| eval(name, proc.binding) }
  proxies.merge! lproxies

  # include arguments
  args, aproxies = dump_values(proc.arguments) { |_, i| procargs[i] }
  proxies.merge! aproxies

  # include dependency loader
  dep_loading = "Marshal.load(#{Marshal.dump(dependencies).inspect}).load!"

  # get the code for the proc
  proc = "proc{ #{proc.to_ruby_without_proc_wrapper} }"
  messenger = "RobotArmy::Messenger.new($stdin, $stdout)"
  context = "RobotArmy::Proxy.new(#{messenger}, #{context.hash.inspect})"

  code = %{
    #{dep_loading} # load dependencies
    #{(locals+args).join("\n")} # all local variables
    #{context}.__proxy_instance_eval(&#{proc}) # run the block
  }

  options[:file] = file
  options[:line] = line
  options[:code] = code
  options[:user] = command.user if command.user

  return options, proxies
end