Class: BirthControl::Report

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

Instance Method Summary collapse

Constructor Details

#initialize(results) ⇒ Report

Returns a new instance of Report.



3
4
5
# File 'lib/birth_control/report.rb', line 3

def initialize(results)
  @results = results
end

Instance Method Details

#class_instantiation_reportObject



40
41
42
43
44
45
46
47
48
# File 'lib/birth_control/report.rb', line 40

def class_instantiation_report
  lines = ["*** [BirthControl] Class instantiation overview ***"]
  @results.each do |klass, _|
    count = "%5s" % count_class(klass)
    lines << "#{count} #{klass.pluralize}"
  end
  lines << "#{"%5s" % count} Total"
  lines.join("\n") + "\n"
end

#countObject



15
16
17
# File 'lib/birth_control/report.rb', line 15

def count
  @results.inject(0) { |c, (klass, _)| c + count_class(klass) }
end

#count_class(klass) ⇒ Object



11
12
13
# File 'lib/birth_control/report.rb', line 11

def count_class(klass)
  @results[klass].inject(0) { |c, (id, _)| c + count_class_and_id(klass, id) }
end

#count_class_and_id(klass, id) ⇒ Object



7
8
9
# File 'lib/birth_control/report.rb', line 7

def count_class_and_id(klass, id)
  @results[klass][id].size
end

#duplication_reportObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/birth_control/report.rb', line 19

def duplication_report
  lines = []

  @results.each do |klass, entries|
    entries.each do |id, callers|
      if callers.size > 1
        lines << "\n#{klass}(#{id}) - #{callers.size} times"
        callers.each_with_index do |caller, index|
          occurence = caller.detect { |call| call.match(/app\//) }
          occurence.sub!(/(.*)\/app\//, 'app/')
          lines << "  #{index+1}. #{occurence}"
        end
      end
    end
  end
  if lines.size > 0
    lines.unshift "*** [BirthControl] Duplicate instantiation detected ***"
    lines.join("\n") + "\n"
  end
end

#to_sObject



50
51
52
53
54
55
56
57
# File 'lib/birth_control/report.rb', line 50

def to_s
  lines = [
    '[BirthControl]',
    class_instantiation_report,
    duplication_report
  ]
  lines.join "\n"
end