123456789_123456789_123456789_123456789_123456789_

Class: CodeStatistics

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: railties/lib/rails/code_statistics.rb

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.new(*pairs) ⇒ CodeStatistics

[ GitHub ]

  
# File 'railties/lib/rails/code_statistics.rb', line 19

def initialize(*pairs)
  @pairs      = pairs
  @statistics = calculate_statistics
  @total      = calculate_total if pairs.length > 1
end

Instance Method Details

#calculate_code (private)

[ GitHub ]

  
# File 'railties/lib/rails/code_statistics.rb', line 65

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 43

def calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|ts|css|scss|coffee|rake|erb)$/)
  stats = 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 39

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 71

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 59

def calculate_total
  @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, total|
    total.add(pair.last)
  end
end

#to_s

[ GitHub ]

  
# File 'railties/lib/rails/code_statistics.rb', line 25

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

#width_for(label) (private)

[ GitHub ]

  
# File 'railties/lib/rails/code_statistics.rb', line 77

def width_for(label)
  [@statistics.values.sum { |s| s.public_send(label) }.to_s.size, HEADERS[label].length].max
end