engine.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ##
  2. ## $Release: 2.7.0 $
  3. ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
  4. ##
  5. require 'erubis/generator'
  6. require 'erubis/converter'
  7. require 'erubis/evaluator'
  8. require 'erubis/context'
  9. module Erubis
  10. ##
  11. ## (abstract) abstract engine class.
  12. ## subclass must include evaluator and converter module.
  13. ##
  14. class Engine
  15. #include Evaluator
  16. #include Converter
  17. #include Generator
  18. def initialize(input=nil, properties={})
  19. #@input = input
  20. init_generator(properties)
  21. init_converter(properties)
  22. init_evaluator(properties)
  23. @src = convert(input) if input
  24. end
  25. ##
  26. ## convert input string and set it to @src
  27. ##
  28. def convert!(input)
  29. @src = convert(input)
  30. end
  31. ##
  32. ## load file, write cache file, and return engine object.
  33. ## this method create code cache file automatically.
  34. ## cachefile name can be specified with properties[:cachename],
  35. ## or filname + 'cache' is used as default.
  36. ##
  37. def self.load_file(filename, properties={})
  38. cachename = properties[:cachename] || (filename + '.cache')
  39. properties[:filename] = filename
  40. timestamp = File.mtime(filename)
  41. if test(?f, cachename) && timestamp == File.mtime(cachename)
  42. engine = self.new(nil, properties)
  43. engine.src = File.read(cachename)
  44. else
  45. input = File.open(filename, 'rb') {|f| f.read }
  46. engine = self.new(input, properties)
  47. tmpname = cachename + rand().to_s[1,8]
  48. File.open(tmpname, 'wb') {|f| f.write(engine.src) }
  49. File.rename(tmpname, cachename)
  50. File.utime(timestamp, timestamp, cachename)
  51. end
  52. engine.src.untaint # ok?
  53. return engine
  54. end
  55. ##
  56. ## helper method to convert and evaluate input text with context object.
  57. ## context may be Binding, Hash, or Object.
  58. ##
  59. def process(input, context=nil, filename=nil)
  60. code = convert(input)
  61. filename ||= '(erubis)'
  62. if context.is_a?(Binding)
  63. return eval(code, context, filename)
  64. else
  65. context = Context.new(context) if context.is_a?(Hash)
  66. return context.instance_eval(code, filename)
  67. end
  68. end
  69. ##
  70. ## helper method evaluate Proc object with contect object.
  71. ## context may be Binding, Hash, or Object.
  72. ##
  73. def process_proc(proc_obj, context=nil, filename=nil)
  74. if context.is_a?(Binding)
  75. filename ||= '(erubis)'
  76. return eval(proc_obj, context, filename)
  77. else
  78. context = Context.new(context) if context.is_a?(Hash)
  79. return context.instance_eval(&proc_obj)
  80. end
  81. end
  82. end # end of class Engine
  83. ##
  84. ## (abstract) base engine class for Eruby, Eperl, Ejava, and so on.
  85. ## subclass must include generator.
  86. ##
  87. class Basic::Engine < Engine
  88. include Evaluator
  89. include Basic::Converter
  90. include Generator
  91. end
  92. class PI::Engine < Engine
  93. include Evaluator
  94. include PI::Converter
  95. include Generator
  96. end
  97. end