Parent

Class Index [+]

Quicksearch

Erubis::TinyEruby

tiny and the simplest implementation of eRuby

ex.

  eruby = TinyEruby.new(File.read('example.rhtml'))
  print eruby.src                 # print ruby code
  print eruby.result(binding())   # eval ruby code with Binding object
  print eruby.evalute(context)    # eval ruby code with context object

Constants

EMBEDDED_PATTERN

Attributes

src[R]

Public Class Methods

new(input=nil) click to toggle source
    # File lib/erubis/tiny.rb, line 19
19:     def initialize(input=nil)
20:       @src = convert(input) if input
21:     end

Public Instance Methods

convert(input) click to toggle source
    # File lib/erubis/tiny.rb, line 26
26:     def convert(input)
27:       src = "_buf = '';"           # preamble
28:       pos = 0
29:       input.scan(EMBEDDED_PATTERN) do |indicator, code|
30:         m = Regexp.last_match
31:         text = input[pos...m.begin(0)]
32:         pos  = m.end(0)
33:         #src << " _buf << '" << escape_text(text) << "';"
34:         text.gsub!(/['\\]/, '\\\&')
35:         src << " _buf << '" << text << "';" unless text.empty?
36:         if !indicator              # <% %>
37:           src << code << ";"
38:         elsif indicator == '#'     # <%# %>
39:           src << ("\n" * code.count("\n"))
40:         else                       # <%= %>
41:           src << " _buf << (" << code << ").to_s;"
42:         end
43:       end
44:       #rest = $' || input                        # ruby1.8
45:       rest = pos == 0 ? input : input[pos..1]   # ruby1.9
46:       #src << " _buf << '" << escape_text(rest) << "';"
47:       rest.gsub!(/['\\]/, '\\\&')
48:       src << " _buf << '" << rest << "';" unless rest.empty?
49:       src << "\n_buf.to_s\n"       # postamble
50:       return src
51:     end
evaluate(_context=Object.new) click to toggle source
    # File lib/erubis/tiny.rb, line 61
61:     def evaluate(_context=Object.new)
62:       if _context.is_a?(Hash)
63:         _obj = Object.new
64:         _context.each do |k, v| _obj.instance_variable_set("@#{k}", v) end
65:         _context = _obj
66:       end
67:       _context.instance_eval @src
68:     end
result(_binding=TOPLEVEL_BINDING) click to toggle source

def escape_text(text)

  return text.gsub!(/['\\]/, '\\\\\&') || text

end

    # File lib/erubis/tiny.rb, line 57
57:     def result(_binding=TOPLEVEL_BINDING)
58:       eval @src, _binding
59:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.