Class: Hotch::Memory::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/hotch/memory.rb

Defined Under Namespace

Classes: Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results, ignore_paths) ⇒ Report

Returns a new instance of Report.



89
90
91
92
93
94
# File 'lib/hotch/memory.rb', line 89

def initialize(results, ignore_paths)
  @header = Line.new(*Line.members)
  @lines = results.map do |result|
    Line.from_result(result, ignore_paths)
  end.compact
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



87
88
89
# File 'lib/hotch/memory.rb', line 87

def lines
  @lines
end

Instance Method Details

#+(other) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hotch/memory.rb', line 96

def +(other)
  by_key = Hash[@lines.map { |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

#formatObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/hotch/memory.rb', line 109

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) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/hotch/memory.rb', line 120

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_sObject



128
129
130
131
132
# File 'lib/hotch/memory.rb', line 128

def to_s
  io = StringIO.new
  puts(io)
  io.string
end