Sha256: f3811dd602c95ab26790a66518ba832290ffc04f5d6e28bcf48bbebd723bd035

Contents?: true

Size: 1.11 KB

Versions: 18

Compression:

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

18 entries across 18 versions & 2 rubygems

Version Path
staticmatic2-2.1.8 lib/staticmatic/template_error.rb
staticmatic2-2.1.7 lib/staticmatic/template_error.rb
staticmatic2-2.1.6 lib/staticmatic/template_error.rb
staticmatic2-2.1.5 lib/staticmatic/template_error.rb
staticmatic2-2.1.4 lib/staticmatic/template_error.rb
staticmatic2-2.1.3 lib/staticmatic/template_error.rb
staticmatic2-2.1.2 lib/staticmatic/template_error.rb
staticmatic2-2.1.1 lib/staticmatic/template_error.rb
staticmatic2-2.1.0 lib/staticmatic/template_error.rb
staticmatic2-2.0.2 lib/staticmatic/template_error.rb
staticmatic2-2.0.1 lib/staticmatic/template_error.rb
staticmatic2-2.0.0 lib/staticmatic/template_error.rb
staticmatic-0.11.1 lib/staticmatic/template_error.rb
staticmatic-0.11.0 lib/staticmatic/template_error.rb
staticmatic-0.11.0.alpha.10 lib/staticmatic/template_error.rb
staticmatic-0.11.0.alpha.9 lib/staticmatic/template_error.rb
staticmatic-0.11.0.alpha.8 lib/staticmatic/template_error.rb
staticmatic-0.11.0.alpha.7 lib/staticmatic/template_error.rb