Sha256: a1f673671a51c77b6c63c57c66c47afdb3eddd58e8fbbe89ace3215342287241

Contents?: true

Size: 1.48 KB

Versions: 31

Compression:

Stored size: 1.48 KB

Contents

require 'erb'
require 'nested_error'

class HelpfulERB
  class Error < ::NestedError; end

  # ERB object
  attr_accessor :erb

  attr_accessor :lines_before

  attr_accessor :lines_after

  # Template filename
  attr_accessor :filename

  def initialize(text, filename=nil, opts={})
    @text = text
    @filename = filename
    @lines_before = opts[:before] || 5
    @lines_after = opts[:before] || 1

    @erb = ::ERB.new(@text, nil, '-')
    @erb.filename = @filename if @filename
  end

  def result(binder=nil)
    begin
      return @erb.result(binder)
    rescue Exception => e
      stack = caller 0
      for i in 0..e.backtrace.size
        l = e.backtrace[i]
        #puts "%s %s" % [i, l];
        break if l =~ /^([^:]+):(\d+):in `(render|result)'$/
      end
      template = $1
      line_number = $2.to_i
      raise Exception.new("Caught ERB error but couldn't find line number in backtrace:\n#{e.backtrace.join("\n")}") unless line_number

      lines = @text.split(/\n/)

      min = line_number - @lines_before
      min = 0 if min < 0

      max = line_number + @lines_after
      max = lines.size if max > lines.size

      width = max.to_s.size

      msg = "Problem with template '#{template}' at line #{line_number}:\n"
      for i in min..max
        n = i+1
        marker = n == line_number ? "*" : ""
        msg << "\n%2s %#{width}i %s" % [marker, n, lines[i]]
      end
      msg << "\n\n(#{e.exception.class}) #{e.message}"


      raise NestedError.new(msg, e)
    end
  end
end

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
automate-it-0.9.2 lib/helpful_erb.rb
automate-it-0.9.1 lib/helpful_erb.rb
automate-it-0.9.0 lib/helpful_erb.rb
automateit-0.70923 lib/helpful_erb.rb
automateit-0.70928 lib/helpful_erb.rb
automateit-0.70930 lib/helpful_erb.rb
automateit-0.71003 lib/helpful_erb.rb
automateit-0.71017 lib/helpful_erb.rb
automateit-0.71006 lib/helpful_erb.rb
automateit-0.71012 lib/helpful_erb.rb
automateit-0.71021 lib/helpful_erb.rb
automateit-0.71031.1 lib/helpful_erb.rb
automateit-0.71030 lib/helpful_erb.rb
automateit-0.71031.2 lib/helpful_erb.rb
automateit-0.71101.1 lib/helpful_erb.rb
automateit-0.71101.2 lib/helpful_erb.rb
automateit-0.71102 lib/helpful_erb.rb
automateit-0.71103 lib/helpful_erb.rb
automateit-0.71101 lib/helpful_erb.rb
automateit-0.71031 lib/helpful_erb.rb