Class: LetItGo::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/let_it_go/report.rb

Overview

Turns hash of keys into a semi-inteligable sorted result

Instance Method Summary collapse

Constructor Details

#initialize(hash_of_reports) ⇒ Report

Returns a new instance of Report.



5
6
7
# File 'lib/let_it_go/report.rb', line 5

def initialize(hash_of_reports)
  @hash = hash_of_reports.reject {|k, obj| obj.zero? }
end

Instance Method Details

#countObject



9
10
11
# File 'lib/let_it_go/report.rb', line 9

def count
  @hash.inject(0) {|count, (k, obj)| count + obj.count; }
end


41
42
43
# File 'lib/let_it_go/report.rb', line 41

def print
  puts report
end

#reportObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/let_it_go/report.rb', line 13

def report
  @report = "## Un-Fozen Hotspots (#{count} total)\n\n"

  file_names = @hash.values.map(&:file_name).uniq
  file_name_hash = Hash.new { [] }
  file_names.each do |name|
    file_name_hash[name] = @hash.select {|_, obj| obj.file_name == name}.values.sort {|obj1, obj2| obj1.count <=> obj2.count }.reverse
  end

  file_name_hash = file_name_hash.sort {|(_, objects1), (_, objects2) |
    count1 = objects1.inject(0) {|count, obj| count + obj.count }
    count2 = objects2.inject(0) {|count, obj| count + obj.count }
    count1 <=> count2
    }.reverse

  file_name_hash.each do |file_name, objects|
    count = objects.inject(0) {|count, obj| count + obj.count }
    @report << "  #{count}) #{file_name}\n"
    objects.each do |obj|
      @report << "    - #{obj.count}) #{obj.klass}##{obj.method_name} on line #{ obj.line_number }\n"
    end
  end

  @report << "  (none)" if @hash.empty?
  @report << "\n"
  @report
end