Class: Coverband::Utils::AbsoluteFileConverter

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roots) ⇒ AbsoluteFileConverter

Returns a new instance of AbsoluteFileConverter.



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

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

Class Method Details

.convert(relative_path) ⇒ Object



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

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

.instanceObject



11
12
13
# File 'lib/coverband/utils/absolute_file_converter.rb', line 11

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

.resetObject



15
16
17
# File 'lib/coverband/utils/absolute_file_converter.rb', line 15

def self.reset
  @instance = nil
end

Instance Method Details

#convert(relative_path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/coverband/utils/absolute_file_converter.rb', line 23

def convert(relative_path)
  @cache[relative_path] ||= begin
    relative_filename = relative_path
    local_filename = relative_filename
    @roots.each do |root, root_regexp|
      if relative_filename.match?(root_regexp)
        relative_filename = relative_filename.sub(root_regexp, "./")
        # once we have a relative path break out of the loop
        break
      end
    end

    if relative_filename == local_filename && File.exist?(local_filename)
      real_filename = File.realpath(local_filename)
      @roots.each do |root, root_regexp|
        if real_filename.match?(root_regexp)
          relative_filename = real_filename.sub(root_regexp, "./")
          # once we have a relative path break out of the loop
          break
        end
      end
    end

    # the filename for our reports is expected to be a full path.
    # roots.last should be roots << current_root}/
    # a fully expanded path of config.root
    # filename = filename.gsub('./', roots.last)
    # above only works for app files
    # we need to rethink some of this logic
    # gems aren't at project root and can have multiple locations
    local_root = @roots.find { |root, _root_regexp|
      File.exist?(relative_filename.gsub("./", root))
    }&.first
    local_root ? relative_filename.gsub("./", local_root) : local_filename
  end
end