Class: ActionController::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/amber/rails.rb

Instance Method Summary collapse

Instance Method Details

#amber_execute(code, options = {}) ⇒ Object

The amber_execute executes a string of code with a certein behavior and returns not the result but what was written in the special :render variable of the environment.

The folowing options can be passed to it to influence it’s behaviro.

:language

The language that should be used to compile the code, be adwised that it is nessessary to load this prior to executing it.

:environment

This allows to pass a own environment, for example to publish

variables or to maintain state between executions.
:object

If this is set the code will be executed in the context of the object passed, this using it’s local variable storage and it’s functions. Be aware that when using :object and :environment togehter, variables set in the code will NOT go in the environment as they are local in the object.

:timeout

This is used to limit the execution time of the code, it can be used to prevent runaway processes beeing executed. Very helpfull when the code executed isn’t 100% trunstworty.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/amber/rails.rb', line 68

def amber_execute code, options = {}
  options = {
    :language => :ecma,
    :environment => AmberVM::Interpreter.env
  }.merge(options)
  compiler = AmberVM::Languages[options[:language]].new
  code = compiler.compile(code)
  code = AmberVM::Interpreter::ObjectContext.new(AmberVM::Interpreter::Constant.new(options[:object]), code) if options[:object]
  if options[:environment][:render].is_a? AmberVM::Classes::Error or options[:environment][:render].nil?
    options[:environment][:render] = ""
  end
  puts options[:environment][:render].inspect
  if options[:timeout]
    s = AmberVM::Safety.new :timeout => options[:timeout]
    s.execute(code, options[:environment])
  else
    code.execute(options[:environment])
  end
  options[:environment][:render].val
end

#amber_reder(code, options = {}) ⇒ Object

This function allows to render code that gets compiled by amber. The first parameter passed is the code to compile, a string most likely, the second is a hash that is passed to the render function, see the Rails API doc for details on what to put there.

In addition to those rails specifc parameters amber_render accepts 4 additioal parameters.They are the same as used in amber_execute.



42
43
44
# File 'lib/amber/rails.rb', line 42

def amber_reder code, options = {}
  render options.merge({:text => amber_execute(code, options)})
end