Class: RailsForge::Analyzers::ModelAnalyzer

Inherits:
BaseAnalyzer show all
Defined in:
lib/railsforge/analyzers/model_analyzer.rb

Overview

ModelAnalyzer scans models for issues

Constant Summary collapse

MAX_METHODS =
15
MAX_LINES =
200

Class Method Summary collapse

Methods inherited from BaseAnalyzer

#analyze, #find_rails_app_path

Class Method Details

.analyze(base_path = nil) ⇒ Object

Analyze models



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/railsforge/analyzers/model_analyzer.rb', line 14

def self.analyze(base_path = nil)
  base_path ||= find_rails_app_path
  return [] unless base_path

  models_dir = File.join(base_path, "app", "models")
  return [] unless Dir.exist?(models_dir)

  results = []
  Dir.glob(File.join(models_dir, "**", "*.rb")).each do |file|
    next if file.end_with?("_application.rb")

    content = File.read(file)
    lines = content.lines.count
    methods = content.scan(/def \w+/).count

    issues = []
    issues << "Exceeds #{MAX_LINES} lines" if lines > MAX_LINES
    issues << "Has #{methods} methods" if methods > MAX_METHODS

    if issues.any?
      results << {
        file: File.basename(file),
        path: file,
        lines: lines,
        methods: methods,
        issues: issues
      }
    end
  end

  results
end

.find_rails_app_pathObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/railsforge/analyzers/model_analyzer.rb', line 62

def self.find_rails_app_path
  path = Dir.pwd
  10.times do
    return path if File.exist?(File.join(path, "config", "application.rb"))
    parent = File.dirname(path)
    break if parent == path
    path = parent
  end
  nil
end


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/railsforge/analyzers/model_analyzer.rb', line 47

def self.print_results(results)
  puts "\nModel Analysis"
  puts "-" * 40

  if results.empty?
    puts "✓ No issues found"
    return
  end

  results.each do |result|
    puts "⚠ #{result[:file]}"
    result[:issues].each { |issue| puts "  - #{issue}" }
  end
end