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.
-
#with_tracing(&traced_function) ⇒ TrueClass
Allows to run a block of code in the context of a tracer with the convenient side-effect of not having to remember to turn it off afterwards.
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
321 322 323 324 325 326 |
# File 'lib/trace_spy/method.rb', line 321 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
289 290 291 292 293 |
# File 'lib/trace_spy/method.rb', line 289 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
263 264 265 |
# File 'lib/trace_spy/method.rb', line 263 def disable !!@tracepoint&.disable end |
#enable ⇒ FalseClass
“Enables” the current tracepoint by defining it, caching it, and enabling it
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/trace_spy/method.rb', line 233 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
125 126 127 |
# File 'lib/trace_spy/method.rb', line 125 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
223 224 225 |
# File 'lib/trace_spy/method.rb', line 223 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
158 159 160 |
# File 'lib/trace_spy/method.rb', line 158 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
190 191 192 |
# File 'lib/trace_spy/method.rb', line 190 def on_return(&matcher_fn) @spies[:return] << Qo.match(&matcher_fn) end |
#with_tracing(&traced_function) ⇒ TrueClass
Allows to run a block of code in the context of a tracer with the convenient side-effect of not having to remember to turn it off afterwards.
Tracer will only be active within the block, and will be disabled afterwards
91 92 93 94 95 96 97 |
# File 'lib/trace_spy/method.rb', line 91 def with_tracing(&traced_function) self.enable yield self.disable end |