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.



100
101
102
103
104
105
# 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

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



98
99
100
# File 'lib/hotch/memory.rb', line 98

def lines
  @lines
end

Instance Method Details

#+(other) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# 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

#formatObject



120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object



131
132
133
134
135
136
137
# 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_sObject



139
140
141
142
143
# File 'lib/hotch/memory.rb', line 139

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