1 class CodeStatistics
#:nodoc:
3 TEST_TYPES
= %w(Units Functionals Unit\ tests Functional\ tests Integration\ tests
)
7 @statistics = calculate_statistics
8 @total = calculate_total
if pairs
.length
> 1
13 @pairs.each
{ |pair
| print_line(pair
.first
, @statistics[pair
.first
]) }
17 print_line("Total", @total)
25 def calculate_statistics
26 @pairs.inject({}) { |stats
, pair
| stats
[pair
.first
] = calculate_directory_statistics(pair
.last
); stats
}
29 def calculate_directory_statistics(directory
, pattern
= /.*\.rb$/)
30 stats
= { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
32 Dir
.foreach(directory
) do |file_name
|
33 if File
.stat(directory +
"/" + file_name
).directory
? and (/^\
./ !~ file_name
)
34 newstats
= calculate_directory_statistics(directory +
"/" + file_name
, pattern
)
35 stats
.each
{ |k
, v
| stats
[k
] +
= newstats
[k
] }
38 next unless file_name
=~ pattern
40 f
= File
.open(directory +
"/" + file_name
)
44 stats
["classes"] +
= 1 if line
=~
/class [A-Z]/
45 stats
["methods"] +
= 1 if line
=~
/def [a-z]/
46 stats
["codelines"] +
= 1 unless line
=~
/^\s*$/ || line
=~
/^\s*#/
54 total
= { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
55 @statistics.each_value
{ |pair
| pair
.each
{ |k
, v
| total
[k
] +
= v
} }
61 @statistics.each
{ |k
, v
| code_loc +
= v
['codelines'] unless TEST_TYPES
.include? k
}
67 @statistics.each
{ |k
, v
| test_loc +
= v
['codelines'] if TEST_TYPES
.include? k
}
73 puts
"| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
78 puts
"+----------------------+
-------+
-------+
---------+
---------+
-----+
-------+
"
81 def print_line(name, statistics)
82 m_over_c = (statistics["methods
"] / statistics["classes
"]) rescue m_over_c = 0
83 loc_over_m = (statistics["codelines
"] / statistics["methods
"]) - 2 rescue loc_over_m = 0
85 start = if TEST_TYPES.include? name
86 "| #{name.ljust(20)} "
88 "| #{name.ljust(20)} "
92 "| #{statistics["lines"].to_s.rjust(5)} " +
93 "| #{statistics["codelines"].to_s.rjust(5)} " +
94 "| #{statistics["classes"].to_s.rjust(7)} " +
95 "| #{statistics["methods"].to_s.rjust(7)} " +
96 "| #{m_over_c.to_s.rjust(3)} " +
97 "| #{loc_over_m.to_s.rjust(5)} |"
100 def print_code_test_stats
101 code
= calculate_code
102 tests
= calculate_tests
104 puts
" Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"