class MethodCallTree

Constants

GET_ARGUMENTS
I_LINE
L_LINE
SPACE_SIZE
T_LINE
VERSION

Public Class Methods

create(options = {}, &block) click to toggle source
# File lib/method_call_tree.rb, line 15
def self.create(options = {}, &block)
  new(options, &block)
end
new(options) { || ... } click to toggle source
# File lib/method_call_tree.rb, line 19
def initialize(options)
  @args = options[:args]
  @class = options[:class]
  @tree = {}
  @queue = []

  tracer.enable { yield }

  @tree.each do |root, tree|
    walk(root, tree)
  end
end

Public Instance Methods

to_s() click to toggle source
# File lib/method_call_tree.rb, line 32
def to_s
  @queue.join
end

Private Instance Methods

get_line(end_line:, space:) click to toggle source
# File lib/method_call_tree.rb, line 60
def get_line(end_line:, space:)
  end_line ? "#{space}#{L_LINE} " : "#{space}#{T_LINE} "
end
get_space(end_line:) click to toggle source
# File lib/method_call_tree.rb, line 64
def get_space(end_line:)
  end_line ? ' ' * SPACE_SIZE : I_LINE + ' ' * (SPACE_SIZE - 1)
end
tracer() click to toggle source
# File lib/method_call_tree.rb, line 38
def tracer
  id = 0
  call_stack = []

  TracePoint.new(:call, :return) do |tp|
    case tp.event
      when :call
        key = ''
        key += "#{tp.defined_class}::" if @class
        key += "#{tp.method_id}"
        key += "(#{tp.binding.eval(GET_ARGUMENTS)})" if @args
        key += "_#{id}"
        id += 1

        call_stack.inject(@tree, :[])[key] = {}
        call_stack.push(key)
      when :return
        call_stack.pop
    end
  end
end
walk(parent, tree, space = "") click to toggle source
# File lib/method_call_tree.rb, line 68
def walk(parent, tree, space = "")
  @queue << "#{parent.gsub(/_\d+$/, '')}\n"

  while node = tree.shift
    @queue << get_line(end_line: tree.empty?, space: space)
    walk(*node, space + get_space(end_line: tree.empty?))
  end
end