Class | Erubis::Engine |
In: |
lib/erubis/engine.rb
|
Parent: | Object |
(abstract) abstract engine class. subclass must include evaluator and converter module.
load file, write cache file, and return engine object. this method create code cache file automatically. cachefile name can be specified with properties[:cachename], or filname + ‘cache’ is used as default.
# File lib/erubis/engine.rb, line 49 49: def self.load_file(filename, properties={}) 50: cachename = properties[:cachename] || (filename + '.cache') 51: properties[:filename] = filename 52: if test(?f, cachename) && File.mtime(filename) <= File.mtime(cachename) 53: engine = self.new(nil, properties) 54: engine.src = File.read(cachename) 55: else 56: input = File.open(filename, 'rb') {|f| f.read } 57: engine = self.new(input, properties) 58: File.open(cachename, 'wb') do |f| 59: f.flock(File::LOCK_EX) 60: f.write(engine.src) 61: f.flush() 62: end 63: end 64: engine.src.untaint # ok? 65: return engine 66: end
convert input string and set it to @src
# File lib/erubis/engine.rb, line 38 38: def convert!(input) 39: @src = convert(input) 40: end
helper method to convert and evaluate input text with context object. context may be Binding, Hash, or Object.
# File lib/erubis/engine.rb, line 73 73: def process(input, context=nil, filename=nil) 74: code = convert(input) 75: filename ||= '(erubis)' 76: if context.is_a?(Binding) 77: return eval(code, context, filename) 78: else 79: context = Context.new(context) if context.is_a?(Hash) 80: return context.instance_eval(code, filename) 81: end 82: end
helper method evaluate Proc object with contect object. context may be Binding, Hash, or Object.
# File lib/erubis/engine.rb, line 89 89: def process_proc(proc_obj, context=nil, filename=nil) 90: if context.is_a?(Binding) 91: filename ||= '(erubis)' 92: return eval(proc_obj, context, filename) 93: else 94: context = Context.new(context) if context.is_a?(Hash) 95: return context.instance_eval(&proc_obj) 96: end 97: end