Skip to content

Commit 6655f81

Browse files
committed
ml - add finer-grained aggregate stats
1 parent 21fa8e4 commit 6655f81

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

lib/rails_stats/code_statistics.rb

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ def initialize(root_directory)
1515
@key_concepts = calculate_key_concepts
1616
@projects = calculate_projects
1717
@statistics = calculate_statistics
18-
@total = calculate_total
18+
@code_total, @tests_total, @grand_total = calculate_totals
1919
end
2020

2121
def to_s
2222
print_header
2323
@statistics.each { |key, stats| print_line(key, stats) }
2424
print_splitter
2525

26-
if @total
27-
print_line("Total", @total)
28-
print_splitter
29-
end
26+
print_line("Code", @code_total)
27+
print_line("Tests", @tests_total)
28+
print_line("Total", @grand_total)
29+
print_splitter
3030

3131
print_code_test_stats
3232
end
@@ -111,22 +111,21 @@ def calculate_statistics
111111
out
112112
end
113113

114-
def calculate_total
115-
@statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, total|
116-
total.add(pair.last)
114+
def calculate_totals
115+
# TODO: make this a single loop
116+
code_total = @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, code_total|
117+
code_total.add(pair.last) unless pair.last.test
117118
end
118-
end
119119

120-
def calculate_code
121-
code_loc = 0
122-
@statistics.each { |k, v| code_loc += v.code_lines unless v.test }
123-
code_loc
124-
end
120+
tests_total = @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, tests_total|
121+
tests_total.add(pair.last) if pair.last.test
122+
end
125123

126-
def calculate_tests
127-
test_loc = 0
128-
@statistics.each { |k, v| test_loc += v.code_lines if v.test }
129-
test_loc
124+
grand_total = @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, total|
125+
total.add(pair.last)
126+
end
127+
128+
[code_total, tests_total, grand_total]
130129
end
131130

132131
def print_header
@@ -153,10 +152,8 @@ def print_line(name, statistics)
153152
end
154153

155154
def print_code_test_stats
156-
code = calculate_code
157-
tests = calculate_tests
158-
159-
puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
155+
code_to_test_ratio = @tests_total.code_lines.to_f / @code_total.code_lines
156+
puts " Code LOC: #{@code_total.code_lines} Test LOC: #{@tests_total.code_lines} Code to Test Ratio: 1:#{sprintf("%.1f", code_to_test_ratio)}"
160157
puts ""
161158
end
162159
end

0 commit comments

Comments
 (0)