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 = roots.map { |root| "#{File.expand_path(root)}/" }
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
# 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|
      relative_filename = relative_filename.sub(/^#{root}/, "./")
      # once we have a relative path break out of the loop
      break if relative_filename.start_with? "./"
    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|
      File.exist?(relative_filename.gsub("./", root))
    }
    local_root ? relative_filename.gsub("./", local_root) : local_filename
  end
end