Class: SandboxedErb::TemplateBase

Inherits:
Object
  • Object
show all
Defined in:
lib/sandboxed_erb/template.rb

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initializeTemplateBase

Returns a new instance of TemplateBase.



170
171
172
173
174
175
176
177
178
179
# File 'lib/sandboxed_erb/template.rb', line 170

def initialize
  @_allowed_methods = {}
  self.class.included_modules.each { |mod|
    unless mod == Kernel
      mod.public_instance_methods.each { |m|
        @_allowed_methods[m.intern] = true
      }
    end
  }
end

Instance Method Details

#_get_local(*args) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/sandboxed_erb/template.rb', line 208

def _get_local(*args)
  target = args.shift
  #check if the target is in the context
  if @_locals[target]
    @_locals[target]
  elsif @_allowed_methods[target] #check if the target is defined in one of the mixin helper functions
    begin
      self.send(target, *args)
    rescue Exception=>e
      raise "Error calling #{target}: #{e.message}"
    end
  else
    raise "Unknown method: #{target}"
  end
end

#_sln(line_no) ⇒ Object



224
225
226
# File 'lib/sandboxed_erb/template.rb', line 224

def _sln(line_no)
  @_line = line_no
end

#run(context, locals) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/sandboxed_erb/template.rb', line 181

def run(context, locals)
  context = {} if context.nil?
  context[:locals] = locals
  unless context.nil?
    for k in context.keys
      eval("@#{k} = context[k]")
    end
  end
  @_sb_context = context
  @_locals = locals
  @_line = 1
  begin
    run_internal
  rescue Exception=>e
    raise "Error on line #{@_line}: #{e.message}"
  ensure
    #cleanup the context
    unless context.nil?
      for k in context.keys
        eval("@#{k} = nil")
      end
    end
     @_sb_context = nil
     @_locals = nil
  end
end