123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::Result::SourceFileBuilder

Relationships & Source Files
Inherits: Object
Defined in: lib/simplecov/result/source_file_builder.rb

Overview

Constructs ::SimpleCov::SourceFile instances from a raw coverage hash, sorts them by filename, and surfaces filenames whose source is no longer present on disk so the caller can warn about the silent drop (see #980).

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(original_result, not_loaded_files:) ⇒ SourceFileBuilder

[ GitHub ]

  
# File 'lib/simplecov/result/source_file_builder.rb', line 12

def initialize(original_result, not_loaded_files:)
  @original_result = original_result
  @not_loaded_files = not_loaded_files
  @missing_source_files = []
end

Instance Attribute Details

#missing_source_files (readonly)

[ GitHub ]

  
# File 'lib/simplecov/result/source_file_builder.rb', line 10

attr_reader :missing_source_files

Instance Method Details

#build_source_file(filename, coverage) (private)

[ GitHub ]

  
# File 'lib/simplecov/result/source_file_builder.rb', line 28

def build_source_file(filename, coverage)
  unless File.file?(filename)
    @missing_source_files << filename
    return
  end

  SimpleCov::SourceFile.new(
    filename,
    stringify_outer_keys(coverage),
    loaded: !@not_loaded_files.include?(filename)
  )
end

#call

[ GitHub ]

  
# File 'lib/simplecov/result/source_file_builder.rb', line 18

def call
  SimpleCov::FileList.new(
    @original_result
      .filter_map { |filename, coverage| build_source_file(filename, coverage) }
      .sort_by(&:filename)
  )
end

#stringify_outer_keys(coverage) (private)

Coverage.result returns symbol keys (:lines, :branches, :methods); resultsets loaded from disk are already string-keyed. ::SimpleCov::SourceFile reads with strings, and handles both Array and stringified-Array branch/method keys via restore_ruby_data_structure, so only the outer hash needs normalizing.

[ GitHub ]

  
# File 'lib/simplecov/result/source_file_builder.rb', line 46

def stringify_outer_keys(coverage)
  coverage.transform_keys(&:to_s)
end