Class: PinkSpoon::DefinitionFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/pink_spoon/definition_finder.rb

Overview

Maps a fully-qualified type + method name to a source file and line number inside the installed gem.

Strategy:

1. Convert the type name to a likely gem name (heuristic + Bundler lookup).
2. Find the gem's source directory via Gem::Specification.
3. Convert the type to a file path (Prometheus::Client::Gauge → prometheus/client/gauge.rb).
4. Parse that file with Prism and find the def line.
5. If not found in the direct file, walk parent classes via Prism.

Defined Under Namespace

Classes: DefFinder

Instance Method Summary collapse

Constructor Details

#initialize(root_path) ⇒ DefinitionFinder

Returns a new instance of DefinitionFinder.



16
17
18
19
# File 'lib/pink_spoon/definition_finder.rb', line 16

def initialize(root_path)
  @root_path = root_path
  setup_bundler
end

Instance Method Details

#find(type, method_name) ⇒ Object

Returns { file: absolute_path, line: integer } or nil.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pink_spoon/definition_finder.rb', line 22

def find(type, method_name)
  return nil unless type && method_name

  candidates = candidate_files(type)
  candidates.each do |file|
    next unless File.exist?(file)
    line = method_line(file, method_name.to_s)
    return { file: file, line: line } if line
  end

  nil
end