Sha256: 71f6574730a498607562356b5262ee08dbdd252ada9e62a8c6d710da2c1a09fc

Contents?: true

Size: 838 Bytes

Versions: 10

Compression:

Stored size: 838 Bytes

Contents

# frozen_string_literal: true

module Coverband
  module Utils
    class RelativeFileConverter
      def self.instance
        @instance ||= new(Coverband.configuration.all_root_paths)
      end

      def self.reset
        @instance = nil
      end

      def self.convert(file)
        instance.convert(file)
      end

      def initialize(roots)
        @cache = {}
        @roots = normalize(roots)
      end

      def convert(file)
        @cache[file] ||= begin
          relative_file = file
          @roots.each do |root|
            relative_file = file.gsub(/^#{root}/, ".")
            break relative_file if relative_file.start_with?(".")
          end
          relative_file
        end
      end

      private

      def normalize(paths)
        paths.map { |root| File.expand_path(root) }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
coverband-6.1.4 lib/coverband/utils/relative_file_converter.rb
coverband-6.1.3 lib/coverband/utils/relative_file_converter.rb
coverband-6.1.2 lib/coverband/utils/relative_file_converter.rb
coverband-6.1.2.rc.1 lib/coverband/utils/relative_file_converter.rb
coverband-6.1.1 lib/coverband/utils/relative_file_converter.rb
coverband-6.1.0 lib/coverband/utils/relative_file_converter.rb
coverband-6.0.3.rc.4 lib/coverband/utils/relative_file_converter.rb
coverband-6.0.3.rc.3 lib/coverband/utils/relative_file_converter.rb
coverband-6.0.3.rc.2 lib/coverband/utils/relative_file_converter.rb
coverband-6.0.3.rc.1 lib/coverband/utils/relative_file_converter.rb