Class: Crystalball::MapGenerator::ObjectSourcesDetector::DefinitionTracer

Inherits:
Object
  • Object
show all
Defined in:
lib/crystalball/map_generator/object_sources_detector/definition_tracer.rb

Overview

Class to save paths to classes and modules definitions during code loading. Should be started as soon as possible. Use #constants_definition_paths to fetch traced info

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path) ⇒ DefinitionTracer

Returns a new instance of DefinitionTracer.



11
12
13
14
# File 'lib/crystalball/map_generator/object_sources_detector/definition_tracer.rb', line 11

def initialize(root_path)
  @root_path = root_path
  @constants_definition_paths = {}
end

Instance Attribute Details

#constants_definition_pathsObject

Returns the value of attribute constants_definition_paths.



9
10
11
# File 'lib/crystalball/map_generator/object_sources_detector/definition_tracer.rb', line 9

def constants_definition_paths
  @constants_definition_paths
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



9
10
11
# File 'lib/crystalball/map_generator/object_sources_detector/definition_tracer.rb', line 9

def root_path
  @root_path
end

#trace_pointObject

Returns the value of attribute trace_point.



9
10
11
# File 'lib/crystalball/map_generator/object_sources_detector/definition_tracer.rb', line 9

def trace_point
  @trace_point
end

Instance Method Details

#startObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/crystalball/map_generator/object_sources_detector/definition_tracer.rb', line 16

def start
  self.trace_point ||= TracePoint.new(:class) do |tp|
    mod = tp.self
    path = tp.path

    next unless path&.start_with?(root_path)

    constants_definition_paths[mod] ||= []
    constants_definition_paths[mod] << path
  end.tap(&:enable)
end

#stopObject



28
29
30
31
# File 'lib/crystalball/map_generator/object_sources_detector/definition_tracer.rb', line 28

def stop
  trace_point&.disable
  self.trace_point = nil
end