Class: Web2Go::ERB_Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/Web2Go/ERB_Interpreter.rb

Overview

ERB_Interpreter manages the execution of ERB scripts See eRuby and ERB class in stdlib

Instance Method Summary collapse

Constructor Details

#initializeERB_Interpreter

Returns a new instance of ERB_Interpreter.


11
12
13
# File 'lib/Web2Go/ERB_Interpreter.rb', line 11

def initialize
  @evaluation_context = OpenStruct.new
end

Instance Method Details

#add_field(name, value) ⇒ Object

add a member variable to the evaluation context Can be accessed from within script as @name


23
24
25
# File 'lib/Web2Go/ERB_Interpreter.rb', line 23

def add_field(name,value)
  instance_variable_set(name,value)
end

#add_parameter(name, value) ⇒ Object

Add a named field to the evaluation context Can be accessed from within script as erb.name


17
18
19
# File 'lib/Web2Go/ERB_Interpreter.rb', line 17

def add_parameter(name,value)
  @evaluation_context.send(name+'=',value)
end

#execute(script, safe_mode, params = nil) ⇒ Object

Execute an ERB script and return the resulting string


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/Web2Go/ERB_Interpreter.rb', line 29

def execute(script,safe_mode,params=nil)

  
#      if safe_mode > 0 then
#        script = "<% $SAFE = #{safe_mode};%>" + script
#      end
  
  parser = ERB.new(script)
  
  if !params.nil? then
    params.each { |key,value| add_parameter(key,value) }
  end
  
  erb = @evaluation_context
  # for compatibility with previous usage of ERB in Wiki2Go
  current = @evaluation_context
  
  parser.result(binding)
  
end