Class: Tng::Analyzers::Model

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

Class Method Summary collapse

Class Method Details

.extract_method_names(node, methods, synthetic_methods = [], scopes = [], validations = []) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/tng/analyzers/model.rb', line 89

def self.extract_method_names(node, methods, synthetic_methods = [], scopes = [], validations = [])
  return unless node.is_a?(Prism::Node)

  case node
  when Prism::DefNode
    # Both instance and class methods (def self.method_name)
    methods << node.name.to_s
  when Prism::CallNode
    # Handle scope definitions: scope :name, -> { ... }
    if node.name == :scope && node.arguments&.arguments&.any?
      first_arg = node.arguments.arguments.first
      if first_arg.is_a?(Prism::SymbolNode)
        scope_name = first_arg.value
        scopes << scope_name if scope_name
      end
    # Handle validation macros
    elsif node.name.to_s.start_with?("validate") && node.arguments&.arguments&.any?
      if node.name == :validate
        # Custom validation: validate :method_name -> extract actual method
        node.arguments.arguments.each do |arg|
          if arg.is_a?(Prism::SymbolNode)
            validation_method = arg.value
            validations << validation_method if validation_method
          end
        end
      else
        # Built-in validations: validates :attr, :attr2, ... -> add to validations
        node.arguments.arguments.each do |arg|
          if arg.is_a?(Prism::SymbolNode)
            attr_name = arg.value
            validations << attr_name if attr_name
          end
        end
      end
    end
  when Prism::SingletonClassNode
    # Methods inside class << self blocks
    node.body&.child_nodes&.each do |child|
      extract_method_names(child, methods, synthetic_methods, scopes, validations)
    end
  end

  node.child_nodes.each do |child|
    extract_method_names(child, methods, synthetic_methods, scopes, validations)
  end
end

.files_in_dir(dir = nil) ⇒ Object



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

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

.methods_for_model(model_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tng/analyzers/model.rb', line 26

def self.methods_for_model(model_name)
  raise "model_name is required" if model_name.nil?

  begin
    # Load the model class
    model_class = model_name.constantize

    instance_methods = model_class.public_instance_methods(false)
    class_methods = model_class.public_methods(false) - Class.public_methods

    model_file = Object.const_source_location(model_class.name)&.first

    if model_file && File.exist?(model_file)
      source_code = File.read(model_file)
      result = Prism.parse(source_code)

      defined_methods = []
      synthetic_methods = []
      scopes = []
      validations = []
      extract_method_names(result.value, defined_methods, synthetic_methods, scopes, validations)

      filtered_instance_methods = instance_methods.select do |method_name|
        method = model_class.instance_method(method_name)
        next false unless method.owner == model_class

        defined_methods.include?(method_name.to_s)
      end

      filtered_class_methods = class_methods.select do |method_name|
        defined_methods.include?(method_name.to_s)
      end

      # Return array of hashes with type information
      instance_method_hashes = filtered_instance_methods.map do |name|
        { name: name.to_s, type: "instance_method" }
      end
      class_method_hashes = filtered_class_methods.map do |name|
        { name: name.to_s, type: "class_method" }
      end
      synthetic_method_hashes = synthetic_methods.map do |name|
        { name: name.to_s, type: "synthetic" }
      end
      scope_hashes = scopes.map do |name|
        { name: name.to_s, type: "scope" }
      end
      validation_hashes = validations.map do |name|
        { name: name.to_s, type: "validation" }
      end

      instance_method_hashes + class_method_hashes + synthetic_method_hashes + scope_hashes + validation_hashes
    else
      []
    end
  rescue NameError => e
    puts "❌ Could not load model class #{model_name}: #{e.message}"
    []
  rescue StandardError => e
    puts "❌ Error analyzing model #{model_name}: #{e.message}"
    []
  end
end

.model_connections(model) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/tng/analyzers/model.rb', line 17

def self.model_connections(model)
  raise "model is required" if model.nil?

  file_path = model.is_a?(Hash) ? model[:path] : model
  raise "model path is required" if file_path.nil?

  Tng::Analyzer::Model.model_connections(file_path.to_s)
end

.value_for_model(file_path) ⇒ Object



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

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

  Tng::Analyzer::Model.parse_model_file(file_path)
end