Module: Naught::CallerInfo Private

Defined in:
lib/naught/caller_info.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Utility for parsing Ruby caller/backtrace information

Extracts structured information from caller strings like:

"/path/to/file.rb:42:in `method_name'"
"/path/to/file.rb:42:in `block in method_name'"
"/path/to/file.rb:42:in `block (2 levels) in method_name'"

Class Method Summary collapse

Class Method Details

.adjusted_block_info(block_info, stack, method_name) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adjust block info to show nested levels if applicable

Parameters:

  • block_info (String, nil)

    current block info

  • stack (Array<String>)

    the call stack

  • method_name (String)

    the method name to look for

Returns:

  • (String, nil)

    adjusted block info



112
113
114
115
116
117
# File 'lib/naught/caller_info.rb', line 112

def adjusted_block_info(block_info, stack, method_name)
  return block_info unless simple_block?(block_info)

  levels = count_block_levels(stack, method_name)
  (levels > 1) ? "block (#{levels} levels)" : block_info
end

.count_block_levels(stack, target_method) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Count nested block levels in the call stack

Parameters:

  • stack (Array<String>)

    the call stack

  • target_method (String)

    the method name to look for

Returns:

  • (Integer)

    the number of nested block levels



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/naught/caller_info.rb', line 81

def count_block_levels(stack, target_method)
  stack.reduce(0) do |levels, entry|
    signature = extract_signature(entry.split(":", 3)[2])
    break levels unless signature

    block_info, method_name = parse_signature(signature)

    if method_name == target_method
      block_info&.start_with?("block") ? levels + 1 : (break levels)
    else
      levels
    end
  end
end

.extract_base_label(method_part) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract the base method name from the method part of a caller string

Parameters:

  • method_part (String, nil)

    the third component after splitting on ":"

Returns:

  • (String, nil)

    the extracted method name



52
53
54
55
56
57
58
# File 'lib/naught/caller_info.rb', line 52

def extract_base_label(method_part)
  signature = extract_signature(method_part)
  return nil unless signature

  _block_info, method_name = parse_signature(signature)
  method_name
end

.extract_signature(method_part) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract the full method signature including block info

Parameters:

  • method_part (String, nil)

    the third component after splitting on ":"

Returns:

  • (String, nil)

    the full signature



64
65
66
# File 'lib/naught/caller_info.rb', line 64

def extract_signature(method_part)
  method_part&.match(SIGNATURE_PATTERN)&.[](:signature)
end

.format_caller_for_pebble(stack) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format caller information for display in pebble output

Handles nested block detection by examining the call stack.

Parameters:

  • stack (Array<String>)

    the call stack from Kernel.caller

Returns:

  • (String)

    formatted caller description



37
38
39
40
41
42
43
44
45
46
# File 'lib/naught/caller_info.rb', line 37

def format_caller_for_pebble(stack)
  caller_line = stack.first
  signature = extract_signature(caller_line.split(":", 3)[2])
  return caller_line unless signature

  block_info, method_name = parse_signature(signature)
  block_info = adjusted_block_info(block_info, stack, method_name)

  block_info ? "#{block_info} #{method_name}" : method_name
end

.parse(caller_string) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse a caller string into structured components

Parameters:

  • caller_string (String)

    a single entry from Kernel.caller

Returns:

  • (Hash)

    parsed components with keys :path, :lineno, :base_label



22
23
24
25
26
27
28
29
# File 'lib/naught/caller_info.rb', line 22

def parse(caller_string)
  path, lineno, method_part = caller_string.to_s.split(":", 3)
  {
    path: path,
    lineno: lineno.to_i,
    base_label: extract_base_label(method_part)
  }
end

.parse_signature(signature) ⇒ Array(String, String), Array(nil, String)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse a signature into block info and clean method name

Parameters:

  • signature (String)

    the method signature

Returns:

  • (Array(String, String), Array(nil, String))

    [block_info, method_name]



100
101
102
103
104
# File 'lib/naught/caller_info.rb', line 100

def parse_signature(signature)
  block_info, method_part = split_signature(signature)
  method_name = method_part.split(/[#.]/).last
  [block_info, method_name]
end

.simple_block?(block_info) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if block_info is a simple "block" without level info

Parameters:

  • block_info (String, nil)

Returns:

  • (Boolean)


123
124
125
# File 'lib/naught/caller_info.rb', line 123

def simple_block?(block_info)
  block_info&.start_with?("block") && !block_info.include?("levels")
end

.split_signature(signature) ⇒ Array(String, String), Array(nil, String)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Split a signature into block info and base method name

Parameters:

  • signature (String)

    the method signature

Returns:

  • (Array(String, String), Array(nil, String))

    [block_info, method_name]



72
73
74
# File 'lib/naught/caller_info.rb', line 72

def split_signature(signature)
  signature.include?(" in ") ? signature.split(" in ", 2) : [nil, signature]
end