0.0
No commit activity in last 3 years
No release in over 3 years
Wrap the methods of a class or module to output call info. See a tree of calls made to wrapped methods with argument values and return values.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.14
~> 13.0
~> 0.80.1
 Project Readme

TraceWrapper method call / return tracing

Build Status

TraceWrapper outputs method call and return info for a class or module

Installation

Add this line to your application's Gemfile:

gem 'trace_wrapper'

Or install it generally with:

$ gem install trace_wrapper

Usage

A simple example of a module and a class to be traced:

module MyModule
  module_function

  def something_else
    puts 'What else?'
  end
end

class MyClass
  def self.forty(two: 2)
    40 + two
  end

  def plus_two(x)
    x + 2
  end
end

A basic example of tracing our sample class and module:

TraceWrapper.wrap(MyClass, MyModule) do
  puts MyModule.subject
  puts MyClass.meaning(x: 40)
end

Will output the following (with the tracing all indented by at least 2 spaces):

  MyModule.subject()
  MyModule.subject return "What is the answer …"
What is the answer to the ultimate question?
  MyClass.meaning(x: 40)
    MyClass#plus_two(40)
    MyClass#plus_two return 42
  MyClass.meaning return 42
42

See custom example for a more custom usage

Options

Options for TraceWrapper.new

  • :colour - Enable coloured output (default: nil automatically enables colour if output is a TTY)
  • :output - Specify output IO (default: STDOUT)

Options for TraceWrapper#wrap

  • :method_type - Which type of methods to wrap

It is also supported to run TraceWrapper.wrap with options available to .new and the instance method #wrap.

Sample usage with all options:

tracer = TraceWrapper.new(colour: true, output: STDERR)
tracer.wrap(MyClass, method_type: :instance_methods)
MyClass.new.plus_two(40)
tracer.unwrap

# or in a single call
TraceWrapper.wrap(MyClass, colour: false, method_type: :methods) do
  MyClass.meaning(x: 40)
end

Testing

Run the tests with rake or directly with:

bundle exec ruby test/test_trace_wrapper.rb

Contributing

  1. Fork it
  2. Create your feature branch
  3. Commit your changes
  4. Push to your branch
  5. Create a Pull Request