mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
db045dbbf6
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
71 lines
No EOL
2.1 KiB
Ruby
71 lines
No EOL
2.1 KiB
Ruby
class CodeStatistics
|
|
def initialize(*pairs)
|
|
@pairs = pairs
|
|
@statistics = calculate_statistics
|
|
@total = calculate_total if pairs.length > 1
|
|
end
|
|
|
|
def to_s
|
|
print_header
|
|
@statistics.each{ |k, v| print_line(k, v) }
|
|
print_splitter
|
|
|
|
if @total
|
|
print_line("Total", @total)
|
|
print_splitter
|
|
end
|
|
end
|
|
|
|
private
|
|
def calculate_statistics
|
|
@pairs.inject({}) { |stats, pair| stats[pair.first] = calculate_directory_statistics(pair.last); stats }
|
|
end
|
|
|
|
def calculate_directory_statistics(directory, pattern = /.*rb/)
|
|
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
|
|
|
|
Dir.foreach(directory) do |file_name|
|
|
next unless file_name =~ pattern
|
|
|
|
f = File.open(directory + "/" + file_name)
|
|
|
|
while line = f.gets
|
|
stats["lines"] += 1
|
|
stats["classes"] += 1 if line =~ /class [A-Z]/
|
|
stats["methods"] += 1 if line =~ /def [a-z]/
|
|
stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
|
|
end
|
|
end
|
|
|
|
stats
|
|
end
|
|
|
|
def calculate_total
|
|
total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
|
|
@statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
|
|
total
|
|
end
|
|
|
|
def print_header
|
|
print_splitter
|
|
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
|
|
print_splitter
|
|
end
|
|
|
|
def print_splitter
|
|
puts "+----------------------+-------+-------+---------+---------+-----+-------+"
|
|
end
|
|
|
|
def print_line(name, statistics)
|
|
m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0
|
|
loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0
|
|
|
|
puts "| #{name.ljust(20)} " +
|
|
"| #{statistics["lines"].to_s.rjust(5)} " +
|
|
"| #{statistics["codelines"].to_s.rjust(5)} " +
|
|
"| #{statistics["classes"].to_s.rjust(7)} " +
|
|
"| #{statistics["methods"].to_s.rjust(7)} " +
|
|
"| #{m_over_c.to_s.rjust(3)} " +
|
|
"| #{loc_over_m.to_s.rjust(5)} |"
|
|
end
|
|
end |