class Airbrake::CodeHunk
Represents a small hunk of code consisting of a base line and a couple lines around it @api private
Constants
- MAX_LINE_LEN
@return [Integer] the maximum length of a line
- NLINES
@return [Integer] how many lines should be read around the base line
Public Class Methods
new(config)
click to toggle source
# File lib/airbrake-ruby/code_hunk.rb, line 15 def initialize(config) @config = config end
Public Instance Methods
get(file, line)
click to toggle source
@param [String] file The file to read @param [Integer] line The base line in the file @return [Hash{Integer=>String}, nil] lines of code around the base line
# File lib/airbrake-ruby/code_hunk.rb, line 23 def get(file, line) return unless File.exist?(file) return unless line lines = get_lines(file, [line - NLINES, 1].max, line + NLINES) || {} return { 1 => '' } if lines.empty? lines end
Private Instance Methods
get_from_cache(file)
click to toggle source
# File lib/airbrake-ruby/code_hunk.rb, line 35 def get_from_cache(file) Airbrake::FileCache[file] ||= File.foreach(file) rescue StandardError => ex @config.logger.error( "#{self.class.name}: can't read code hunk for #{file}: #{ex}" ) nil end
get_lines(file, start_line, end_line)
click to toggle source
# File lib/airbrake-ruby/code_hunk.rb, line 44 def get_lines(file, start_line, end_line) return unless (cached_file = get_from_cache(file)) lines = {} cached_file.with_index(1) do |l, i| next if i < start_line break if i > end_line lines[i] = l[0...MAX_LINE_LEN].rstrip end lines end