Class: Tng::Analyzers::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/tng/analyzers/controller.rb

Class Method Summary collapse

Class Method Details

.files_in_dir(dir) ⇒ Object



6
7
8
9
# File 'lib/tng/analyzers/controller.rb', line 6

def self.files_in_dir(dir)
  dir = File.join(Dir.pwd, "app/controllers") if dir.nil?
  Tng::Analyzer::Controller.files_in_dir(dir.to_s)
end

.methods_for_controller(controller_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tng/analyzers/controller.rb', line 45

def self.methods_for_controller(controller_name)
  raise "controller_name is required" if controller_name.nil?

  begin
    # Load the controller class
    controller_class = controller_name.constantize

    # Get public instance methods (actions) excluding inherited ones
    public_methods = controller_class.public_instance_methods(false)

    # Filter to methods defined in this controller only
    action_methods = public_methods.select do |method_name|
      method = controller_class.instance_method(method_name)
      method.owner == controller_class
    end

    # Return method info
    action_methods.map do |method_name|
      {
        name: method_name.to_s
      }
    end
  rescue NameError => e
    puts "❌ Could not load controller class #{controller_name}: #{e.message}"
    []
  rescue StandardError => e
    puts "❌ Error analyzing controller #{controller_name}: #{e.message}"
    []
  end
end

.parents_for_controller(controller) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tng/analyzers/controller.rb', line 23

def self.parents_for_controller(controller)
  raise "controller is required" if controller.nil?

  path = controller[:path]
  relative_path = path.gsub(%r{^.*app/controllers/}, "").gsub(".rb", "")
  clean_controller_name = relative_path.split("/").map(&:camelize).join("::")

  begin
    Tng::Analyzer::Controller.parents_for_controller(clean_controller_name)
  rescue NameError => e
    puts "❌ Error analyzing controller #{clean_controller_name}: #{e.message}"
    puts "   Path: #{path}"
    puts "   This might be due to a syntax error in the controller file."
    []
  rescue StandardError => e
    puts "❌ Error analyzing controller #{clean_controller_name}: #{e.message}"
    puts "   Path: #{path}"
    puts "   This might be due to a syntax error in the controller file."
    []
  end
end

.routes_for_controller(file_path) ⇒ Object



11
12
13
14
15
# File 'lib/tng/analyzers/controller.rb', line 11

def self.routes_for_controller(file_path)
  raise "file_path is required" if file_path.nil?

  Tng::Analyzer::Controller.routes_for_controller(file_path)
end

.value_for_controller(file_path) ⇒ Object



17
18
19
20
21
# File 'lib/tng/analyzers/controller.rb', line 17

def self.value_for_controller(file_path)
  raise "file_path is required" if file_path.nil?

  Tng::Analyzer::Controller.parse_controller_file(file_path)
end