Class: Tng::Analyzers::Service

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

Class Method Summary collapse

Class Method Details

.extract_method_names(node, methods) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tng/analyzers/service.rb', line 82

def self.extract_method_names(node, methods)
  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::SingletonClassNode
    # Methods inside class << self blocks
    node.body&.child_nodes&.each do |child|
      extract_method_names(child, methods)
    end
  end

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

.files_in_dir(dir = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tng/analyzers/service.rb', line 6

def self.files_in_dir(dir = nil)
  base_dirs = if dir.nil?
                ["app/services", "app/service"].select { |d| Dir.exist?(File.join(Dir.pwd, d)) }
              else
                [dir]
              end

  return [] if base_dirs.empty?

  base_dirs.flat_map do |base_dir|
    full_path = File.join(Dir.pwd, base_dir)
    find_service_files(full_path)
  end
end

.find_service_files(dir) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tng/analyzers/service.rb', line 102

def self.find_service_files(dir)
  return [] unless Dir.exist?(dir)

  Dir.glob(File.join(dir, "**", "*.rb")).map do |file_path|
    relative_path = file_path.gsub("#{Dir.pwd}/", "")
    {
      file: File.basename(file_path),
      path: file_path,
      relative_path: relative_path
    }
  end.reject { |file| Tng::Utils.ignored_path?(file[:path]) }
end

.methods_for_service(service_name, file_path = nil) ⇒ Object



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
# File 'lib/tng/analyzers/service.rb', line 27

def self.methods_for_service(service_name, file_path = nil)
  raise "service_name is required" if service_name.nil?

  begin
    # Load the service class
    service_class = service_name.constantize

    instance_methods = service_class.public_instance_methods(false) +
                       service_class.private_instance_methods(false)
    class_methods = service_class.public_methods(false) - Class.public_methods

    # Prefer explicit file path or class definition location
    service_file = file_path
    service_file ||= Object.const_source_location(service_class.name)&.first

    # Fallback to any instance method source location
    if service_file.nil? && instance_methods.any?
      begin
        service_file = service_class.instance_method(instance_methods.first).source_location&.first
      rescue StandardError
        # Method might not have source location, continue
      end
    end

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

                        defined_methods = []
                        extract_method_names(result.value, defined_methods)

                        filtered_instance_methods = instance_methods.select do |method_name|
                          method = service_class.instance_method(method_name)
                          next false unless method.owner == service_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

                        if filtered_instance_methods.empty? && filtered_class_methods.empty? && defined_methods.any?
                          defined_methods
                        else
                          filtered_instance_methods + filtered_class_methods
                        end
                      else
                        []
                      end

    service_methods.map { |method_name| { name: method_name.to_s } }
  end
end

.value_for_service(file_path) ⇒ Object



21
22
23
24
25
# File 'lib/tng/analyzers/service.rb', line 21

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

  Tng::Analyzer::Service.parse_service_file(file_path)
end