Class: SimcovAiFormatter::SourceReader

Inherits:
Object
  • Object
show all
Defined in:
lib/simcov_ai_formatter/source_reader.rb

Overview

Reads source files by 1-indexed line numbers with random access. Files are cached as line arrays after the first read. Missing files and unknown encodings never crash the caller.

Instance Method Summary collapse

Constructor Details

#initialize(warnings: nil) ⇒ SourceReader

Returns a new instance of SourceReader.



6
7
8
9
10
# File 'lib/simcov_ai_formatter/source_reader.rb', line 6

def initialize(warnings: nil)
  @cache = {}
  @missing = []
  @warnings = warnings
end

Instance Method Details

#read(path, start_line, end_line) ⇒ Array<[Integer, String]>?

Returns line-number/text pairs, or nil if the file is missing.

Returns:

  • (Array<[Integer, String]>, nil)

    line-number/text pairs, or nil if the file is missing



13
14
15
16
17
18
19
20
# File 'lib/simcov_ai_formatter/source_reader.rb', line 13

def read(path, start_line, end_line)
  lines = lines_for(path)
  return nil if lines.nil?

  from = [start_line, 1].max
  to = [end_line, lines.size].min
  (from..to).map { |n| [n, lines[n - 1]] }
end

#report_missingObject



22
23
24
25
26
27
# File 'lib/simcov_ai_formatter/source_reader.rb', line 22

def report_missing
  return if @warnings.nil? || @missing.empty?
  @warnings.puts("simcov-ai-formatter: warning: #{@missing.size} source file(s) not found:")
  @missing.first(5).each { |p| @warnings.puts("  - #{p}") }
  @warnings.puts("  ...") if @missing.size > 5
end