Class: TraceSpy::Method
- Inherits:
-
Object
- Object
- TraceSpy::Method
- Defined in:
- lib/trace_spy/method.rb
Overview
Tracer spies all rely on Qo for pattern-matching syntax. In order to more effectively leverage this gem it would be a good idea to look through the Qo documentation present here: github.com/baweaver/qo
Implements a TraceSpy on a Method
Instance Attribute Summary collapse
-
#current_trace ⇒ Object
readonly
The current trace being executed upon, can be used in matcher blocks to get the entire trace context instead of just a part.
Instance Method Summary collapse
-
#current_arguments ⇒ Hash[Symbol, Any]
Returns the arguments of the currently active trace.
-
#current_local_variables ⇒ Hash[Symbol, Any]
Returns the local variables of the currently active trace.
-
#disable ⇒ Boolean
Disables the TracePoint, or pretends it did if one isn’t enabled yet.
-
#enable ⇒ FalseClass
“Enables” the current tracepoint by defining it, caching it, and enabling it.
-
#initialize(method_name, from_class: Any) {|_self| ... } ⇒ TraceSpy::Method
constructor
Creates a new method trace.
-
#on_arguments(&matcher_fn) ⇒ Array[Qo::Matcher]
Creates a Spy on function arguments.
-
#on_exception(&matcher_fn) ⇒ Array[Qo::Matcher]
Creates a Spy on a certain type of exception.
-
#on_locals(&matcher_fn) ⇒ Array[Qo::Matcher]
Creates a Spy on local method variables.
-
#on_return(&matcher_fn) ⇒ Array[Qo::Matcher]
Creates a Spy on function returns.
Constructor Details
#initialize(method_name, from_class: Any) {|_self| ... } ⇒ TraceSpy::Method
Creates a new method trace
58 59 60 61 62 63 64 65 66 |
# File 'lib/trace_spy/method.rb', line 58 def initialize(method_name, from_class: Any, &fn) @method_name = method_name @from_class = from_class @spies = Hash.new { |h,k| h[k] = [] } @tracepoint = nil @current_trace = nil yield(self) if block_given? end |
Instance Attribute Details
#current_trace ⇒ Object (readonly)
The current trace being executed upon, can be used in matcher blocks to get the entire trace context instead of just a part.
40 41 42 |
# File 'lib/trace_spy/method.rb', line 40 def current_trace @current_trace end |
Instance Method Details
#current_arguments ⇒ Hash[Symbol, Any]
This method will attempt to avoid running in contexts where argument retrieval will give a runtime error.
Returns the arguments of the currently active trace
290 291 292 293 294 295 |
# File 'lib/trace_spy/method.rb', line 290 def current_arguments return {} unless @current_trace return {} if RAISE_EVENT.include?(@current_trace.event) extract_args(@current_trace) end |
#current_local_variables ⇒ Hash[Symbol, Any]
Returns the local variables of the currently active trace
258 259 260 261 262 |
# File 'lib/trace_spy/method.rb', line 258 def current_local_variables return {} unless @current_trace extract_locals(@current_trace) end |
#disable ⇒ Boolean
Disables the TracePoint, or pretends it did if one isn’t enabled yet
232 233 234 |
# File 'lib/trace_spy/method.rb', line 232 def disable !!@tracepoint&.disable end |
#enable ⇒ FalseClass
“Enables” the current tracepoint by defining it, caching it, and enabling it
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/trace_spy/method.rb', line 202 def enable @tracepoint = TracePoint.new do |trace| begin next unless matches?(trace) @current_trace = trace call_with = -> with { -> spy { spy.call(with) } } @spies[:arguments].each(&call_with[extract_args(trace)]) if CALL_EVENT.include?(trace.event) @spies[:locals].each(&call_with[extract_locals(trace)]) if LINE_EVENT.include?(trace.event) @spies[:return].each(&call_with[trace.return_value]) if RETURN_EVENT.include?(trace.event) @spies[:exception].each(&call_with[trace.raised_exception]) if RAISE_EVENT.include?(trace.event) @current_trace = nil rescue RuntimeError => e # Stupid hack for now p e end end @tracepoint.enable end |
#on_arguments(&matcher_fn) ⇒ Array[Qo::Matcher]
Creates a Spy on function arguments
94 95 96 |
# File 'lib/trace_spy/method.rb', line 94 def on_arguments(&matcher_fn) @spies[:arguments] << Qo.match(&matcher_fn) end |
#on_exception(&matcher_fn) ⇒ Array[Qo::Matcher]
Creates a Spy on a certain type of exception
192 193 194 |
# File 'lib/trace_spy/method.rb', line 192 def on_exception(&matcher_fn) @spies[:exception] << Qo.match(&matcher_fn) end |
#on_locals(&matcher_fn) ⇒ Array[Qo::Matcher]
Creates a Spy on local method variables
127 128 129 |
# File 'lib/trace_spy/method.rb', line 127 def on_locals(&matcher_fn) @spies[:locals] << Qo.match(&matcher_fn) end |
#on_return(&matcher_fn) ⇒ Array[Qo::Matcher]
Creates a Spy on function returns
159 160 161 |
# File 'lib/trace_spy/method.rb', line 159 def on_return(&matcher_fn) @spies[:return] << Qo.match(&matcher_fn) end |