Skip to content

Commit d4bb309

Browse files
committed
Adjust stats, formatter and calculator to print schema/structure file information
1 parent 8f2e5b7 commit d4bb309

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

lib/rails_stats/console_formatter.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def to_s
1818
end
1919

2020
print_code_test_stats
21+
print_schema_stats
2122
end
2223

2324
private
@@ -53,5 +54,15 @@ def print_code_test_stats
5354
puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)} Files: #{calculator.files_total}"
5455
puts ""
5556
end
57+
58+
def print_schema_stats
59+
if File.exist?(calculator.schema_path)
60+
puts " Schema Stats: #{calculator.schema} `create_table` calls in schema.rb"
61+
elsif File.exist?(calculator.structure_path)
62+
puts " Schema Stats: #{calculator.schema} `CREATE TABLE` calls in structure.sql"
63+
else
64+
puts " Schema Stats: No schema.rb or structure.sql file found"
65+
end
66+
end
5667
end
5768
end

lib/rails_stats/json_formatter.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def result
1717
@result << stat_hash("Tests", @tests_total).merge(code_test_hash) if @tests_total
1818
@result << stat_hash("Total", @grand_total).merge(code_test_hash) if @grand_total
1919

20+
@result << { "schema_stats" => schema_info }
21+
2022
@result
2123
end
2224

@@ -50,5 +52,16 @@ def stat_hash(name, statistics)
5052
"loc_over_m" => loc_over_m.to_s
5153
}
5254
end
55+
56+
def schema_info
57+
if File.exist?(calculator.schema_path)
58+
{
59+
"schema_stats" => calculator.schema,
60+
"create_table_calls" => calculator.create_table_calls
61+
}
62+
else
63+
{ "schema_stats" => "No schema.rb file found" }
64+
end
65+
end
5366
end
5467
end

lib/rails_stats/stats_calculator.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ class StatsCalculator
99

1010
def initialize(root_directory)
1111
@root_directory = root_directory
12+
@schema_path = File.join(@root_directory, "db", "schema.rb")
13+
@structure_path = File.join(@root_directory, "db", "structure.sql")
1214
@key_concepts = calculate_key_concepts
1315
@projects = calculate_projects
1416
@statistics = calculate_statistics
1517
@code_loc = calculate_code
1618
@test_loc = calculate_tests
19+
@schema = calculate_create_table_calls
1720
@files_total, @code_total, @tests_total, @grand_total = calculate_totals
1821
end
1922

20-
attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total
23+
attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total, :schema, :schema_path, :structure_path
2124

2225
private
2326

@@ -133,5 +136,23 @@ def calculate_tests
133136
@statistics.each { |k, v| @test_loc += v.code_lines if v.test }
134137
@test_loc
135138
end
139+
140+
def calculate_create_table_calls
141+
if @schema_path && File.exist?(@schema_path)
142+
count_create_table_calls(schema_path, "create_table")
143+
elsif @structure_path && File.exist?(@structure_path)
144+
count_create_table_calls(structure_path, "CREATE TABLE")
145+
else
146+
0
147+
end
148+
end
149+
150+
def count_create_table_calls(file_path, keyword)
151+
create_table_count = 0
152+
File.foreach(file_path) do |line|
153+
create_table_count += 1 if line.strip.start_with?(keyword)
154+
end
155+
create_table_count
156+
end
136157
end
137158
end

0 commit comments

Comments
 (0)