Class: Coverband::Utils::RelativeFileConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/coverband/utils/relative_file_converter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roots) ⇒ RelativeFileConverter

Returns a new instance of RelativeFileConverter.



18
19
20
21
# File 'lib/coverband/utils/relative_file_converter.rb', line 18

def initialize(roots)
  @cache = {}
  @roots_regexp = Regexp.union(convert_roots(roots))
end

Class Method Details

.convert(file) ⇒ Object



14
15
16
# File 'lib/coverband/utils/relative_file_converter.rb', line 14

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

.instanceObject



6
7
8
# File 'lib/coverband/utils/relative_file_converter.rb', line 6

def self.instance
  @instance ||= new(Coverband.configuration.all_root_paths)
end

.resetObject



10
11
12
# File 'lib/coverband/utils/relative_file_converter.rb', line 10

def self.reset
  @instance = nil
end

Instance Method Details

#convert(file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/coverband/utils/relative_file_converter.rb', line 23

def convert(file)
  @cache[file] ||= begin
    relative_file = file.sub(@roots_regexp, "./")

    if relative_file == file && !file.start_with?(".") && File.exist?(file)
      real_file = File.realpath(file)
      new_relative_file = real_file.sub(@roots_regexp, "./")
      relative_file = ((new_relative_file == real_file) ? file : new_relative_file)
    end

    relative_file
  end
end