class Hotch::Memory::Report

Attributes

lines[R]

Public Class Methods

new(results, ignore_paths) click to toggle source
# File lib/hotch/memory.rb, line 100
def initialize(results, ignore_paths)
  @header = Line.new(*Line.members)
  @lines = results.map do |result|
    Line.from_result(result, ignore_paths)
  end.compact
end

Public Instance Methods

+(other) click to toggle source
# File lib/hotch/memory.rb, line 107
def +(other)
  by_key = @lines.to_h { |line| [line.key, line] }
  other.lines.each do |line|
    if (existing = by_key[line.key])
      existing.sum(line)
    else
      by_key[line.key] = line
      @lines << line
    end
  end
  self
end
format() click to toggle source
# File lib/hotch/memory.rb, line 120
def format
  # TODO: refactor
  max_lengths = Array.new(Line.members.size, 0)
  ([@header, @total] + @lines).each do |line|
    line.lengths.each.with_index do |length, i|
      max_lengths[i] = length if length > max_lengths[i]
    end
  end
  max_lengths.map { |len| "%#{len}s" }.join(" ")
end
puts(io) click to toggle source
# File lib/hotch/memory.rb, line 131
def puts(io)
  total!
  fmt = format
  @header.puts(io, fmt)
  @lines.sort_by(&:count).each { |line| line.puts(io, fmt) }
  @total.puts(io, fmt)
end
to_s() click to toggle source
# File lib/hotch/memory.rb, line 139
def to_s
  io = StringIO.new
  puts(io)
  io.string
end

Private Instance Methods

total!() click to toggle source
# File lib/hotch/memory.rb, line 204
def total!
  return if defined? @total

  @total = Line::Total.new
  @lines.each do |line|
    @total.sum(line)
  end
end