Sha256: 7042c2008ea4d0c379085ffac2135363c53265a047ca301b64e432ba0b2b5327

Contents?: true

Size: 1.04 KB

Versions: 9

Compression:

Stored size: 1.04 KB

Contents

class StaticMatic::TemplateError < StandardError
  SOURCE_CODE_RADIUS = 3
  
  attr_reader :original_exception, :backtrace

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

    @source = File.read(template)
  end
  
  # TODO: Replace 'haml|sass' with any registered engines
  def line_number
    @line_number ||= $2 if backtrace.find { |line| line =~ /\((haml|sass)\)\:(\d+)/ }
  end
  
  def filename
    @template
  end
  
  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
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
bdimcheff-staticmatic-0.10.1 lib/staticmatic/template_error.rb
mattt-staticmatic-0.10.1 lib/staticmatic/template_error.rb
staticmatic-0.10.6 lib/staticmatic/template_error.rb
staticmatic-0.10.5 lib/staticmatic/template_error.rb
staticmatic-0.10.4 lib/staticmatic/template_error.rb
staticmatic-0.10.3 lib/staticmatic/template_error.rb
staticmatic-0.10.2 lib/staticmatic/template_error.rb
staticmatic-0.10.0 lib/staticmatic/template_error.rb
staticmatic-0.10.1 lib/staticmatic/template_error.rb