Class: Erubis::Eruby
- Inherits:
-
Object
- Object
- Erubis::Eruby
- Defined in:
- lib/erubis.rb
Overview
base class
Direct Known Subclasses
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#src ⇒ Object
readonly
Returns the value of attribute src.
Class Method Summary collapse
Instance Method Summary collapse
-
#compile(input) ⇒ Object
private.
- #evaluate(_context = {}) ⇒ Object
-
#initialize(input, options = {}) ⇒ Eruby
constructor
A new instance of Eruby.
- #result(binding = TOPLEVEL_BINDING) ⇒ Object
Constructor Details
#initialize(input, options = {}) ⇒ Eruby
Returns a new instance of Eruby.
61 62 63 64 65 66 67 |
# File 'lib/erubis.rb', line 61 def initialize(input, ={}) #@input = input @pattern = [:pattern] || '<% %>' @filename = [:filename] @trim = [:trim] != false @src = compile(input) end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
69 70 71 |
# File 'lib/erubis.rb', line 69 def filename @filename end |
#src ⇒ Object (readonly)
Returns the value of attribute src.
68 69 70 |
# File 'lib/erubis.rb', line 68 def src @src end |
Class Method Details
.load_file(filename, options = {}) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/erubis.rb', line 71 def self.load_file(filename, ={}) input = File.open(filename, 'rb') { |f| f.read } [:filename] = filename eruby = self.new(input, ) return eruby end |
Instance Method Details
#compile(input) ⇒ Object
private
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/erubis.rb', line 94 def compile(input) src = "" initialize_src(src) prefix, postfix = @pattern.split() regexp = /(.*?)(^[ \t]*)?#{prefix}(=+|\#)?(.*?)#{postfix}([ \t]*\r?\n)?/m input.scan(regexp) do |text, head_space, indicator, code, tail_space| ## * when '<%= %>', do nothing ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>' if indicator && indicator[0] == ?= flag_trim = false else flag_trim = @trim && head_space && tail_space end #flag_trim = @trim && !(indicator && indicator[0]==?=) && head_space && tail_space add_src_text(src, text) add_src_text(src, head_space) if !flag_trim && head_space if !indicator # <% %> code = "#{head_space}#{code}#{tail_space}" if flag_trim add_src_code(src, code) elsif indicator[0] == ?= # <%= %> add_src_expr(src, code, indicator) else # <%# %> n = code.count("\n") n += tail_space.count("\n") if tail_space add_src_code(src, "\n" * n) end add_src_text(src, tail_space) if !flag_trim && tail_space end rest = $' || input add_src_text(src, rest) finalize_src(src) return src end |
#evaluate(_context = {}) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/erubis.rb', line 83 def evaluate(_context={}) _evalstr = '' _context.keys.each do |key| _evalstr << "#{key.to_s} = _context[#{key.inspect}]\n" end eval _evalstr return result(binding()) end |
#result(binding = TOPLEVEL_BINDING) ⇒ Object
78 79 80 81 |
# File 'lib/erubis.rb', line 78 def result(binding=TOPLEVEL_BINDING) filename = @filename || '(erubis)' eval @src, binding, filename end |