Exception: StaticMatic::TemplateError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/staticmatic/template_error.rb

Constant Summary

SOURCE_CODE_RADIUS =
3

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (TemplateError) initialize(template, original_exception)

Returns a new instance of TemplateError



6
7
8
9
10
11
12
13
14
15
# File 'lib/staticmatic/template_error.rb', line 6

def initialize(template, original_exception)
  @template, @original_exception = template, original_exception
  @backtrace = original_exception.backtrace
  
  if template
    @source = File.read(template)
  else 
    @source = ""
  end
end

Instance Attribute Details

- (Object) backtrace (readonly)

Returns the value of attribute backtrace



4
5
6
# File 'lib/staticmatic/template_error.rb', line 4

def backtrace
  @backtrace
end

- (Object) original_exception (readonly)

Returns the value of attribute original_exception



4
5
6
# File 'lib/staticmatic/template_error.rb', line 4

def original_exception
  @original_exception
end

Instance Method Details

- (Object) filename



22
23
24
# File 'lib/staticmatic/template_error.rb', line 22

def filename
  @template
end

- (Object) line_number

TODO: Replace 'haml|sass' with any registered engines



18
19
20
# File 'lib/staticmatic/template_error.rb', line 18

def line_number
  @line_number ||= $2 if backtrace.find { |line| line =~ /\((haml|sass|scss)\)\:(\d+)/ }
end

- (Object) source_extract(indentation = 0)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/staticmatic/template_error.rb', line 26

def source_extract(indentation = 0)
  return "" unless num = line_number
  num = num.to_i

  source_code = @source.split("\n")

  start_on_line = [ num - SOURCE_CODE_RADIUS - 1, 0 ].max
  end_on_line   = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min

  indent = ' ' * indentation
  line_counter = start_on_line
  return unless source_code = source_code[start_on_line..end_on_line]

  source_code.collect do |line|
    line_counter += 1
    "#{indent}#{line_counter}: #{line}\n"
  end.to_s
end