Class: Erubis::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/erubis/engine.rb

Overview

(abstract) abstract engine class. subclass must include evaluator and converter module.

Direct Known Subclasses

Basic::Engine, PI::Engine

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil, properties = {}) ⇒ Engine

include Evaluator include Converter include Generator



26
27
28
29
30
31
32
# File 'lib/erubis/engine.rb', line 26

def initialize(input=nil, properties={})
  #@input = input
  init_generator(properties)
  init_converter(properties)
  init_evaluator(properties)
  @src    = convert(input) if input
end

Class Method Details

.load_file(filename, properties = {}) ⇒ Object

load file, write cache file, and return engine object. this method create code cache file automatically. cachefile name can be specified with properties, or filname + ‘cache’ is used as default.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/erubis/engine.rb', line 49

def self.load_file(filename, properties={})
  cachename = properties[:cachename] || (filename + '.cache')
  properties[:filename] = filename
  if test(?f, cachename) && File.mtime(filename) <= File.mtime(cachename)
    engine = self.new(nil, properties)
    engine.src = File.read(cachename)
  else
    input = File.open(filename, 'rb') {|f| f.read }
    engine = self.new(input, properties)
    File.open(cachename, 'wb') do |f|
      f.flock(File::LOCK_EX)
      f.write(engine.src)
      f.flush()
    end
  end
  engine.src.untaint   # ok?
  return engine
end

Instance Method Details

#convert!(input) ⇒ Object

convert input string and set it to @src



38
39
40
# File 'lib/erubis/engine.rb', line 38

def convert!(input)
  @src = convert(input)
end

#process(input, context = nil, filename = nil) ⇒ Object

helper method to convert and evaluate input text with context object. context may be Binding, Hash, or Object.



73
74
75
76
77
78
79
80
81
82
# File 'lib/erubis/engine.rb', line 73

def process(input, context=nil, filename=nil)
  code = convert(input)
  filename ||= '(erubis)'
  if context.is_a?(Binding)
    return eval(code, context, filename)
  else
    context = Context.new(context) if context.is_a?(Hash)
    return context.instance_eval(code, filename)
  end
end

#process_proc(proc_obj, context = nil, filename = nil) ⇒ Object

helper method evaluate Proc object with contect object. context may be Binding, Hash, or Object.



89
90
91
92
93
94
95
96
97
# File 'lib/erubis/engine.rb', line 89

def process_proc(proc_obj, context=nil, filename=nil)
  if context.is_a?(Binding)
    filename ||= '(erubis)'
    return eval(proc_obj, context, filename)
  else
    context = Context.new(context) if context.is_a?(Hash)
    return context.instance_eval(&proc_obj)
  end
end