class MiniErb

A simplified, streamlined erb replacement.

Constants

DESCRIPTION
VERSION

Attributes

filename[RW]

The optional file name used when the mini_erb code is run.

lineno[RW]

The optional line number used when the mini_erb code is run.

src[R]

The Ruby code generated by mini erb.

Public Class Methods

new(string, safe_level=nil, _=nil, eoutvar='_erbout') click to toggle source

Create a mini_erb object. Spurious parameter is for erb compatibility.

# File lib/mini_erb.rb, line 22
def initialize(string, safe_level=nil, _=nil, eoutvar='_erbout')
  @safe_level, @eoutvar = safe_level, eoutvar
  @filename, @lineno = "(mini_erb)", 0
  @src = compile(string)
end

Public Instance Methods

compile(string) click to toggle source

Compile the mini erb input

# File lib/mini_erb.rb, line 29
def compile(string)
  buffer, suppress = [], false

  until string.empty?
    text, code, string = string.partition(/<%.*?%>/m)

    text.sub!(/\A$\r?\n?/, "") if suppress
    buffer << "#{@eoutvar}<<#{text.inspect};" unless text.empty?

    unless code.empty?
      end_point = (suppress = code[-3] == "-") ? -3 : -2

      unless code[2] == "#"
        if code[2] == "="
          buffer << "#{@eoutvar}<<(#{code[3...end_point]}).to_s;"
        else
          buffer << "#{code[2...end_point]};"
        end
      end
    end
  end

  @eoutvar + "=String.new;" + buffer.join + @eoutvar
end
location=((filename, lineno)) click to toggle source

Set the file name and optional line number associated with the compiled code.

# File lib/mini_erb.rb, line 16
def location=((filename, lineno))
  @filename = filename
  @lineno = lineno if lineno
end
result(evaluator = new_toplevel) click to toggle source

Return the mini erb text with embedded Ruby results.

# File lib/mini_erb.rb, line 55
def result(evaluator = new_toplevel)
  if @safe_level
    proc {$SAFE = @safe_level; eval(@src, evaluator, @filename, @lineno)}.call
  else
    eval(@src, evaluator, @filename, @lineno)
  end
end
run(evaluator = new_toplevel) click to toggle source

Generate results and print them.

# File lib/mini_erb.rb, line 64
def run(evaluator = new_toplevel)
  print result(evaluator)
end

Private Instance Methods

new_toplevel() click to toggle source

Get a duplicate of the default binding if none is specified.

# File lib/mini_erb.rb, line 71
def new_toplevel
  TOPLEVEL_BINDING.dup
end