Class: Rails::CodeStatistics
Relationships & Source Files | |
Inherits: | Object |
Defined in: | railties/lib/rails/code_statistics.rb |
Constant Summary
-
DIRECTORIES =
# File 'railties/lib/rails/code_statistics.rb', line 8[ %w(Controllers app/controllers), %w(Helpers app/helpers), %w(Jobs app/jobs), %w(Models app/models), %w(Mailers app/mailers), %w(Mailboxes app/mailboxes), %w(Channels app/channels), %w(Views app/views), %w(JavaScripts app/assets/javascripts), %w(Stylesheets app/assets/stylesheets), %w(JavaScript app/javascript), %w(Libraries lib/), %w(APIs app/apis), %w(Controller\ tests test/controllers), %w(Helper\ tests test/helpers), %w(Job\ tests test/jobs), %w(Model\ tests test/models), %w(Mailer\ tests test/mailers), %w(Mailbox\ tests test/mailboxes), %w(Channel\ tests test/channels), %w(Integration\ tests test/integration), %w(System\ tests test/system),
-
HEADERS =
# File 'railties/lib/rails/code_statistics.rb', line 43{ lines: " Lines", code_lines: " LOC", classes: "Classes", methods: "Methods" }
-
TEST_TYPES =
# File 'railties/lib/rails/code_statistics.rb', line 33["Controller tests", "Helper tests", "Model tests", "Mailer tests", "Mailbox tests", "Channel tests", "Job tests", "Integration tests", "System tests"]
Class Attribute Summary
Class Method Summary
- .new(*pairs) ⇒ CodeStatistics constructor
-
.register_directory(label, path, test_directory: false)
Add directories to the output of the
bin/rails stats
command.
Instance Attribute Summary
Instance Method Summary
- #to_s
- #calculate_code private
- #calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|ts|css|scss|coffee|rake|erb)$/) private
- #calculate_statistics private
- #calculate_tests private
- #calculate_total private
- #print_code_test_stats private
- #print_header private
- #print_line(name, statistics) private
- #print_splitter private
- #width_for(label) private
Constructor Details
.new(*pairs) ⇒ CodeStatistics
# File 'railties/lib/rails/code_statistics.rb', line 60
def initialize(*pairs) @pairs = pairs @statistics = calculate_statistics @total = calculate_total if pairs.length > 1 end
Class Attribute Details
.directories (rw)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 45
class_attribute :directories, default: DIRECTORIES
.directories? ⇒ Boolean
(rw)
[ GitHub ]
# File 'railties/lib/rails/code_statistics.rb', line 45
class_attribute :directories, default: DIRECTORIES
.test_types (rw)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 46
class_attribute :test_types, default: TEST_TYPES
.test_types? ⇒ Boolean
(rw)
[ GitHub ]
# File 'railties/lib/rails/code_statistics.rb', line 46
class_attribute :test_types, default: TEST_TYPES
Class Method Details
.register_directory(label, path, test_directory: false)
Add directories to the output of the bin/rails stats
command.
Rails::CodeStatistics.register_directory("My Directory", "path/to/dir")
For directories that contain test code, set the test_directory
argument to true.
Rails::CodeStatistics.register_directory("Model specs", "spec/models", test_directory: true)
# File 'railties/lib/rails/code_statistics.rb', line 55
def self.register_directory(label, path, test_directory: false) self.directories << [label, path] self.test_types << label if test_directory end
Instance Attribute Details
#directories (rw)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 45
class_attribute :directories, default: DIRECTORIES
#directories? ⇒ Boolean
(rw)
[ GitHub ]
# File 'railties/lib/rails/code_statistics.rb', line 45
class_attribute :directories, default: DIRECTORIES
#test_types (rw)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 46
class_attribute :test_types, default: TEST_TYPES
#test_types? ⇒ Boolean
(rw)
[ GitHub ]
# File 'railties/lib/rails/code_statistics.rb', line 46
class_attribute :test_types, default: TEST_TYPES
Instance Method Details
#calculate_code (private)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 106
def calculate_code code_loc = 0 @statistics.each { |k, v| code_loc += v.code_lines unless test_types.include? k } code_loc end
#calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|ts|css|scss|coffee|rake|erb)$/) (private)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 84
def calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|ts|css|scss|coffee|rake|erb)$/) stats = Rails::CodeStatisticsCalculator.new Dir.foreach(directory) do |file_name| path = "#{directory}/#{file_name}" if File.directory?(path) && !file_name.start_with?(".") stats.add(calculate_directory_statistics(path, pattern)) elsif file_name&.match?(pattern) stats.add_by_file_path(path) end end stats end
#calculate_statistics (private)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 80
def calculate_statistics Hash[@pairs.map { |pair| [pair.first, calculate_directory_statistics(pair.last)] }] end
#calculate_tests (private)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 112
def calculate_tests test_loc = 0 @statistics.each { |k, v| test_loc += v.code_lines if test_types.include? k } test_loc end
#calculate_total (private)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 100
def calculate_total @statistics.each_with_object(Rails::CodeStatisticsCalculator.new) do |pair, total| total.add(pair.last) end end
#print_code_test_stats (private)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 151
def print_code_test_stats code = calculate_code tests = calculate_tests puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f / code)}" puts "" end
#print_header (private)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 122
def print_header print_splitter print "| Name " HEADERS.each do |k, v| print " | #{v.rjust(width_for(k))}" end puts " | M/C | LOC/M |" print_splitter end
#print_line(name, statistics) (private)
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 140
def print_line(name, statistics) m_over_c = (statistics.methods / statistics.classes) rescue 0 loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue 0 print "| #{name.ljust(20)} " HEADERS.each_key do |k| print "| #{statistics.send(k).to_s.rjust(width_for(k))} " end puts "| #{m_over_c.to_s.rjust(3)} | #{loc_over_m.to_s.rjust(5)} |" end
#print_splitter (private)
[ GitHub ]#to_s
[ GitHub ]# File 'railties/lib/rails/code_statistics.rb', line 66
def to_s print_header @pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) } print_splitter if @total print_line("Total", @total) print_splitter end print_code_test_stats end