Sha256: fa76c3a920253ecb893f2186be9fb95f90bbae419b4f3de1579fdbf8cbe70ba4

Contents?: true

Size: 1.01 KB

Versions: 31

Compression:

Stored size: 1.01 KB

Contents

module Raven
  class LineCache
    def initialize
      @cache = {}
    end

    # Any linecache you provide to Raven must implement this method.
    # Returns an Array of Strings representing the lines in the source
    # file. The number of lines retrieved is (2 * context) + 1, the middle
    # line should be the line requested by lineno. See specs for more information.
    def get_file_context(filename, lineno, context)
      return nil, nil, nil unless valid_path?(filename)
      lines = Array.new(2 * context + 1) do |i|
        getline(filename, lineno - context + i)
      end
      [lines[0..(context - 1)], lines[context], lines[(context + 1)..-1]]
    end

    private

    def valid_path?(path)
      lines = getlines(path)
      !lines.nil?
    end

    def getlines(path)
      @cache[path] ||= begin
        IO.readlines(path)
      rescue
        nil
      end
    end

    def getline(path, n)
      return nil if n < 1
      lines = getlines(path)
      return nil if lines.nil?
      lines[n - 1]
    end
  end
end

Version data entries

31 entries across 31 versions & 3 rubygems

Version Path
sentry-raven-2.6.2 lib/raven/linecache.rb
sentry-raven-2.6.1 lib/raven/linecache.rb
sentry-raven-2.6.0 lib/raven/linecache.rb
sentry-raven-2.5.3 lib/raven/linecache.rb
sentry-raven-2.5.2 lib/raven/linecache.rb
sentry-raven-2.5.1 lib/raven/linecache.rb
sentry-raven-2.5.0 lib/raven/linecache.rb
sentry-raven-2.4.0 lib/raven/linecache.rb
sentry-raven-2.3.1 lib/raven/linecache.rb
sentry-raven-2.3.0 lib/raven/linecache.rb
sentry-raven-2.2.0 lib/raven/linecache.rb