Class: Hone::MethodMap

Inherits:
Object
  • Object
show all
Defined in:
lib/hone/method_map.rb

Overview

Maps method definitions to their line ranges for correlating hotspots with specific methods in Ruby source files.

Defined Under Namespace

Classes: MethodExtractor, MethodInfo

Instance Method Summary collapse

Constructor Details

#initializeMethodMap

Returns a new instance of MethodMap.



19
20
21
# File 'lib/hone/method_map.rb', line 19

def initialize
  @methods = []
end

Instance Method Details

#add_file(path) ⇒ Object

Parse a Ruby file and extract all method definitions



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hone/method_map.rb', line 24

def add_file(path)
  normalized_path = File.expand_path(path)
  result = Prism.parse_file(normalized_path)

  if result.errors.any?
    warn "Parse errors in #{normalized_path}:"
    result.errors.each { |e| warn "  #{e.message}" }
    return self
  end

  extractor = MethodExtractor.new(normalized_path)
  result.value.accept(extractor)
  @methods.concat(extractor.methods)

  self
end

#all_methodsObject

Return all registered methods



60
61
62
# File 'lib/hone/method_map.rb', line 60

def all_methods
  @methods.dup
end

#method_at(file, line) ⇒ Object

Find the method that contains a given line in a file



42
43
44
45
46
47
48
# File 'lib/hone/method_map.rb', line 42

def method_at(file, line)
  normalized_file = File.expand_path(file)

  @methods.find do |method|
    method.file == normalized_file && method.contains_line?(line)
  end
end

#methods_in_file(file) ⇒ Object

Return all methods for a given file



51
52
53
54
55
56
57
# File 'lib/hone/method_map.rb', line 51

def methods_in_file(file)
  normalized_file = File.expand_path(file)

  @methods.select do |method|
    method.file == normalized_file
  end
end