Class: HelpfulERB
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#erb ⇒ Object
ERB object.
-
#filename ⇒ Object
Template filename.
-
#lines_after ⇒ Object
Returns the value of attribute lines_after.
-
#lines_before ⇒ Object
Returns the value of attribute lines_before.
Instance Method Summary collapse
-
#initialize(text, filename = nil, opts = {}) ⇒ HelpfulERB
constructor
A new instance of HelpfulERB.
- #result(binder = nil) ⇒ Object
Constructor Details
#initialize(text, filename = nil, opts = {}) ⇒ HelpfulERB
Returns a new instance of HelpfulERB.
17 18 19 20 21 22 23 24 25 |
# File 'lib/helpful_erb.rb', line 17 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 |
Instance Attribute Details
#filename ⇒ Object
Template filename
15 16 17 |
# File 'lib/helpful_erb.rb', line 15 def filename @filename end |
#lines_after ⇒ Object
Returns the value of attribute lines_after.
12 13 14 |
# File 'lib/helpful_erb.rb', line 12 def lines_after @lines_after end |
#lines_before ⇒ Object
Returns the value of attribute lines_before.
10 11 12 |
# File 'lib/helpful_erb.rb', line 10 def lines_before @lines_before end |
Instance Method Details
#result(binder = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/helpful_erb.rb', line 27 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.}" raise NestedError.new(msg, e) end end |