Module: AmazingTap

Defined in:
lib/amazing_tap.rb,
lib/amazing_tap/version.rb,
sig/amazing_tap.rbs

Defined Under Namespace

Modules: ObjectExtension Classes: Error

Constant Summary collapse

DEFAULT_CLOSING_THRESHOLD =
24
RECEIVER_PATTERN =
/([\w@$.\[\]!?]+)\s*\.\s*tap(?:p|ap)\b/
VERSION =

Returns:

  • (String)
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.closing_thresholdObject



19
20
21
# File 'lib/amazing_tap.rb', line 19

def closing_threshold
  @closing_threshold ||= DEFAULT_CLOSING_THRESHOLD
end

Class Method Details

.infer_label(location) ⇒ String?

Extracts the receiver expression preceding .tapp/.tapap on the call-site line, e.g. "user" for user.tapp or "account.owner" for account.owner.tapp. Inference is textual: literals, parenthesized calls, multiline chains, or unreadable sources (eval, REPL) yield nil.

Parameters:

  • location (Thread::Backtrace::Location)

    the tapp call site

Returns:

  • (String, nil)

    the receiver expression, or nil when not inferable



30
31
32
33
34
35
36
# File 'lib/amazing_tap.rb', line 30

def infer_label(location)
  path = location.path
  return nil unless path && File.readable?(path)

  line = File.readlines(path)[location.lineno - 1]
  line && line[RECEIVER_PATTERN, 1]
end

This method returns an undefined value.

Prints a header line (label and/or class) followed by the ap-formatted object, then optional caller frames. Output longer than .closing_threshold lines is closed with a "/label" line so long dumps stay identifiable. Colorized under the same conditions amazing_print colorizes its output.

Parameters:

  • object (Object)

    the object to print

  • label (Symbol, String, nil)

    label for the header; nil prints a class-only header (or none with type: false)

  • callers (Array<String>)

    backtrace of the tapp call site

  • type (Boolean) (defaults to: true)

    append the object's class to the header

  • backtrace (Boolean, Integer) (defaults to: false)

    print all caller frames (true) or the first N frames (Integer) after the object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/amazing_tap.rb', line 52

def print_labeled(object, label, callers:, type: true, backtrace: false)
  colorize = AmazingPrint::Inspector.new.colorize?

  header = []
  header << (colorize ? AmazingPrint::Colors.yellow(label.to_s) : label.to_s) if label
  if type
    type_text = "(#{object.class})"
    header << (colorize ? AmazingPrint::Colors.whiteish(type_text) : type_text)
  end
  puts header.join(" ") unless header.empty?

  body = object.ai
  puts body

  frames = backtrace == true ? callers : callers.first(backtrace || 0)
  frames.each { |frame| puts "  from #{frame}" }

  return unless label && body.lines.length + frames.length > closing_threshold

  closing = "/#{label}"
  puts colorize ? AmazingPrint::Colors.yellow(closing) : closing
end