class CallGraph::Instrument

Constants

INTROSPECT

Attributes

file_path[RW]
set[RW]

Public Class Methods

new(file_path: default_file_path) click to toggle source
# File lib/call_graph/instrument.rb, line 10
def initialize(file_path: default_file_path)
  @file_path      = file_path
  @set            = Set.new
end

Public Instance Methods

path(kind) click to toggle source
# File lib/call_graph/instrument.rb, line 15
def path(kind)
  "#{file_path}.#{kind}"
end
trace() { || ... } click to toggle source
# File lib/call_graph/instrument.rb, line 19
def trace(&block)
  trace_point.enable
  yield
  trace_point.disable

  File.open(path(:tmp), 'w') { |fd| fd.write set.to_a.compact.join("\n") }

  set.clear
end

Private Instance Methods

default_file_path() click to toggle source
# File lib/call_graph/instrument.rb, line 31
def default_file_path
  'call-graph'
end
trace_point() click to toggle source
# File lib/call_graph/instrument.rb, line 35
def trace_point
  @trace_point ||= begin
    TracePoint.new(:call) do |trace|
      next if trace.defined_class == self.class

      case trace.event
      when :call
        id       = trace.method_id
        caller   = trace.binding.of_caller(2).eval(INTROSPECT)
        receiver = trace.binding.eval(INTROSPECT)

        next if caller == receiver
      
        set.add("#{caller},#{receiver},#{id}")
      end
    end
  end
end