Module: ActsAsRunnableCode::InstanceMethods
- Defined in:
- lib/acts_as_runnable_code.rb
Instance Method Summary collapse
-
#run_code(toplevel_object = nil, options = {}) ⇒ Object
Run the user’s code in the context of a toplevel_object The toplevel_object will be automatically wrapped and copied into the sandbox.
-
#sandbox ⇒ Object
Get the sandbox used to eval user code for this class Makes sandbox references to the wrapper classes.
Instance Method Details
#run_code(toplevel_object = nil, options = {}) ⇒ Object
Run the user’s code in the context of a toplevel_object The toplevel_object will be automatically wrapped and copied into the sandbox
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/acts_as_runnable_code.rb', line 10 def run_code(toplevel_object = nil, = {}) [:timeout] ||= self.class.[:timeout] s = sandbox if toplevel_object s.set(:toplevel, ActsAsWrappedClass::WrapperFinder.find_wrapper_for(toplevel_object)) s.set(:code, self.send(self.class.source_code_field)) result = s.eval("eval(code, toplevel.send(:binding))", ) else s.set(:code, self.send(self.class.source_code_field)) result = s.eval("eval(code)", ) end result.class.respond_to?(:wrapper_class?) && result.class.wrapper_class? ? result._wrapped_object : result end |
#sandbox ⇒ Object
Get the sandbox used to eval user code for this class Makes sandbox references to the wrapper classes
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/acts_as_runnable_code.rb', line 26 def sandbox return self.class.instance_variable_get("@sandbox") if self.class.instance_variables.include?("@sandbox") if self.class.[:unsafe_sandbox] s = Sandbox.new else s = Sandbox.safe end classes = self.class.[:classes] || ActsAsWrappedClass::WRAPPED_CLASSES classes.each do |c| name = "#{c}Wrapper" s.ref eval(name) end self.class.instance_variable_set("@sandbox", s) if self.class.[:singleton_sandbox] s end |