Exception: StaticMatic::TemplateError

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

Constant Summary collapse

SOURCE_CODE_RADIUS =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, original_exception) ⇒ TemplateError

Returns a new instance of TemplateError.



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

def initialize(template, original_exception)
  @template, @original_exception = template, original_exception
  @backtrace = original_exception.backtrace

  @source = File.read(template)
end

Instance Attribute Details

#backtraceObject (readonly)

Returns the value of attribute backtrace.



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

def backtrace
  @backtrace
end

#original_exceptionObject (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

#filenameObject



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

def filename
  @template
end

#line_numberObject

TODO: Replace ‘haml|sass’ with any registered engines



14
15
16
# File 'lib/staticmatic/template_error.rb', line 14

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

#source_extract(indentation = 0) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/staticmatic/template_error.rb', line 22

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