blob: 889fa4947fc29fa42378a35da7e11acd072eaf5f (
plain)
1
2
3
4
5
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
##
## $Rev$
## $Release:$
## $Copyright$
##
require 'erubis/error'
require 'erubis/context'
module Erubis
##
## evaluate code
##
module Evaluator
def self.supported_properties # :nodoc:
return []
end
attr_accessor :src, :filename
def init_evaluator(properties)
@filename = properties[:filename]
end
def result(*args)
raise NotSupportedError.new("evaluation of code except Ruby is not supported.")
end
def evaluate(*args)
raise NotSupportedError.new("evaluation of code except Ruby is not supported.")
end
end
##
## evaluator for Ruby
##
module RubyEvaluator
include Evaluator
def self.supported_properties # :nodoc:
list = Evaluator.supported_properties
return list
end
## eval(@src) with binding object
def result(_binding_or_hash=TOPLEVEL_BINDING)
_arg = _binding_or_hash
if _arg.is_a?(Hash)
## load _context data as local variables by eval
#eval _arg.keys.inject("") { |s, k| s << "#{k.to_s} = _arg[#{k.inspect}];" }
eval _arg.collect{|k,v| "#{k} = _arg[#{k.inspect}]; "}.join
_arg = binding()
end
return eval(@src, _arg, (@filename || '(erubis)'))
end
## invoke context.instance_eval(@src)
def evaluate(context=Context.new)
context = Context.new(context) if context.is_a?(Hash)
#return context.instance_eval(@src, @filename || '(erubis)')
@_proc ||= eval("proc { #{@src} }", TOPLEVEL_BINDING, @filename || '(erubis)')
return context.instance_eval(&@_proc)
end
## define method to module_object. this is equivarent to ERB#def_method.
def def_method(module_object, method_name, filename=nil)
module_object.module_eval("def #{method_name}; #{@src}; end", filename || @filename)
end
end
end
|