Class: SimpleCov::Formatter::VimFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov-vim/formatter.rb

Instance Method Summary collapse

Instance Method Details

#common_directory(files) ⇒ Object



25
26
27
28
29
# File 'lib/simplecov-vim/formatter.rb', line 25

def common_directory(files)
  File::join(files.map{|file| file.split(File::Separator)}.inject do |dir, path|
    dir.zip(path).take_while{|l,r| l == r}.map{|l,_| l}
  end)
end

#format(result) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simplecov-vim/formatter.rb', line 3

def format(result)
  results = {}
  dir_re = /^#{common_directory(result.filenames)}\//
  result.filenames.zip(result.original_result.values_at(*result.filenames)).each do |name, lines|
    results[name.sub(dir_re, "")] = file_results = {hits: [], misses: []}
    lines.each_with_index do |hits, line|
      case hits
      when nil
      when 0
        file_results[:misses] << line + 1
      else
        file_results[:hits] << line + 1
      end
    end
  end

  coverage_output = "coverage.vim"
  write_file(template("coverage.vim"), coverage_output, binding)

  puts "Wrote vim coverage script to #{coverage_output}"
end

#template(name) ⇒ Object



39
40
41
# File 'lib/simplecov-vim/formatter.rb', line 39

def template(name)
  ERB.new(File.read(File.join(File.dirname(__FILE__), "#{name}.erb")), nil, '-')
end

#write_file(template, output_filename, binding) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/simplecov-vim/formatter.rb', line 31

def write_file(template, output_filename, binding)
  content = template.result( binding )

  File.open( output_filename, "w" ) do |file_result|
    file_result.write content
  end
end