Class: TraceWrapper

Inherits:
Object
  • Object
show all
Includes:
Shell
Defined in:
lib/trace_wrapper.rb,
lib/trace_wrapper/shell.rb,
lib/trace_wrapper/version.rb

Overview

Wraps methods on given classes or modules to output a call/return tree.

Defined Under Namespace

Modules: Shell

Constant Summary collapse

VERSION =

Current version of TraceWrapper

'0.2.0'

Constants included from Shell

Shell::COLOURS, Shell::ELLIPSIS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shell

#colour, #colour?

Constructor Details

#initialize(output: $stdout, colour: nil) ⇒ TraceWrapper

Create a new TraceWrapper

Options:

:output - IO object to write trace output to (default STDOUT) :colour - True to use shell colours in output (default nil will colour if output is a TTY)



35
36
37
38
39
40
41
42
# File 'lib/trace_wrapper.rb', line 35

def initialize(output: $stdout, colour: nil)
  @output = output
  @colour = colour
  @unwrappers = []
  @main_process_id = process_id
  @processes = {}
  process
end

Class Method Details

.wrap(*receivers, **options, &block) ⇒ Object

Wraps methods on given receivers with tracing

options will be passed to .new and #wrap respectively

If a block is given, it will be passed to #wrap



18
19
20
21
22
23
24
# File 'lib/trace_wrapper.rb', line 18

def wrap(*receivers, **options, &block) # :yields: a_trace_wrapper
  init_keys = %i[output colour]
  init_args = options.select { |k, _| init_keys.include?(k) }
  wrap_args = options.reject { |k, _| init_keys.include?(k) }

  new(**init_args).wrap(*receivers, **wrap_args, &block)
end

Instance Method Details

#unwrapObject

Remove any wrappers set by this tracer



88
89
90
91
# File 'lib/trace_wrapper.rb', line 88

def unwrap
  @unwrappers.each(&:call)
  @unwrappers = []
end

#wrap(*receivers, method_type: :all, visibility: :protected) ⇒ Object

Wraps methods on given receivers with tracing

Options

:method_type - Types of methods to wrap (default: :all). Choices are: :instance (for instance methods), :self (for receiver methods, i.e. class/module functions), :all for both

:visibility - Lowest method visibility level to wrap (default: :protected). Choices are: :public, :protected, :private.

If a block is given, the wrappers will be created just around the block and the block’s result will be returned. The TraceWrapper instance will be yielded to the block to allow further wraps to be added.

If no block is given, you should call unwrap after use.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/trace_wrapper.rb', line 63

def wrap(*receivers,
         method_type: :all,
         visibility: :protected) # :yields: a_trace_wrapper
  unwrappers = []
  Array(*receivers).each do |receiver|
    if %i[all self].include?(method_type)
      unwrappers += wrap_methods(receiver, visibility: visibility)
    end
    if %i[all instance].include?(method_type)
      unwrappers += wrap_instance_methods(receiver, visibility: visibility)
    end
  end
  if block_given?
    begin
      yield(self)
    ensure
      unwrappers.each(&:call)
    end
  else
    @unwrappers += unwrappers
    self
  end
end