Exception: CommonMob::Erb::ErbError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/common_mob/erb.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, ex) ⇒ ErbError

Returns a new instance of ErbError.



29
30
31
32
33
# File 'lib/common_mob/erb.rb', line 29

def initialize(file,ex)
  @file = file
  @src = file.read
  @ex  = ex
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



57
58
59
# File 'lib/common_mob/erb.rb', line 57

def method_missing(meth,*args,&block)
  @ex.send(meth,*args,&block)
end

Instance Method Details

#error_with_context(ctx = 2) ⇒ Object



42
43
44
45
46
47
# File 'lib/common_mob/erb.rb', line 42

def error_with_context(ctx=2)
  start_on_line = [ line_number - ctx - 1, 0          ].max
  end_on_line   = [ line_number + ctx    , @src.length].min

  @src.split("\n")[ start_on_line..end_on_line ]
end

#line_numberObject



49
50
51
# File 'lib/common_mob/erb.rb', line 49

def line_number
  @line_number = line_number!
end

#line_number!Object



52
53
54
55
# File 'lib/common_mob/erb.rb', line 52

def line_number!
  re = /\(erb\):(\d+):in `get_binding'/
  @line_number = $1.to_i if @ex.backtrace.find {|line| line =~ re}
end

#messageObject



35
36
37
# File 'lib/common_mob/erb.rb', line 35

def message
  "error rendering erb: #{@ex.message}\nat line #{line_number+1} of #{@file}\n#{error_with_context * "\n"}"
end

#to_sObject



38
39
40
# File 'lib/common_mob/erb.rb', line 38

def to_s
  message
end