Sha256: 134f32575bc62704d6d739e9202ad56dc473bb0439e45e00162eefccf9e2824e

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module TraceLocation
  module Collector # :nodoc:
    require_relative 'event'
    Result = Struct.new(:events, :return_value)

    def self.collect(&block)
      events = []
      hierarchy = 0
      tracer = TracePoint.new(:call, :return) do |trace_point|
        next unless trace_point.path.include?(::TraceLocation.config.gems_dir)

        case trace_point.event
        when :call
          events << Event.new(
            event: trace_point.event,
            path: trace_point.path,
            lineno: trace_point.lineno,
            method_id: trace_point.method_id,
            defined_class: trace_point.defined_class,
            hierarchy: hierarchy
          )

          hierarchy += 1
        when :return
          hierarchy -= 1

          events << Event.new(
            event: trace_point.event,
            path: trace_point.path,
            lineno: trace_point.lineno,
            method_id: trace_point.method_id,
            defined_class: trace_point.defined_class,
            hierarchy: hierarchy
          )
        end
      end
      return_value = tracer.enable { block.call }
      Result.new(events, return_value)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trace_location-0.4.0 lib/trace_location/collector.rb
trace_location-0.3.0 lib/trace_location/collector.rb