class Coveralls::Cobertura::Reader

Constants

XML_OPTIONS

Attributes

filename[R]
number_of_classes[R]

Public Class Methods

new(filename) click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 9
def initialize(filename)
  unless File.exists?(filename)
    raise ArgumentError.new("Expecting file named #{filename} to exist.")
  end
  @filename = filename
  @number_of_classes = classes.size
end

Public Instance Methods

class_coverages() click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 17
def class_coverages
  @class_coverages ||= filenames.map do |filename|
    class_coverage(filename)
  end
end

Private Instance Methods

array_from_collection(collection) click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 67
def array_from_collection(collection)
  case collection
  when Hash
    [collection]
  when Array
    collection
  else
    []
  end
end
class_coverage(filename) click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 24
def class_coverage(filename)
  ClassCoverage.new(filename, coverage_for_filename(filename))
end
classes() click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 45
def classes
  packages.map do |package|
    array_from_collection(package[:classes][:class])
  end.flatten
end
cobertura() click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 59
def cobertura
  @cobertura ||= MultiXml.parse(file, XML_OPTIONS)
end
coverage() click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 55
def coverage
  @coverage ||= cobertura[:coverage] unless cobertura.nil?
end
coverage_for_filename(filename) click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 28
def coverage_for_filename(filename)
  lines = classes.select do |e|
    e[:filename] == filename
  end.map do |e|
    e[:lines][:line]
  end.flatten
  max_line = lines.map{ |line| line[:number].to_i }.max
  (1..max_line).map do |index|
    found_line = lines.find { |line| line[:number] == index.to_s }
    (found_line.nil?) ? nil : found_line[:hits].to_i
  end
end
file() click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 63
def file
  @file ||= File.new(filename)
end
filenames() click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 41
def filenames
  @filenames ||= classes.map { |e| e[:filename] }.uniq
end
packages() click to toggle source
# File lib/coveralls/cobertura/reader.rb, line 51
def packages
  @packages ||= array_from_collection(coverage[:packages][:package])
end