Class: IvarTracer

Inherits:
Tracer::Base show all
Defined in:
lib/tracer/ivar_tracer.rb

Constant Summary

Constants inherited from Tracer::Base

Tracer::Base::DIR, Tracer::Base::HOME, Tracer::Base::M_CLASS, Tracer::Base::M_INSPECT, Tracer::Base::M_INSTANCE_VARIABLE_GET, Tracer::Base::M_IS_A, Tracer::Base::M_OBJECT_ID

Constants included from Tracer::Color

Tracer::Color::BLUE, Tracer::Color::BOLD, Tracer::Color::CLEAR, Tracer::Color::CYAN, Tracer::Color::GREEN, Tracer::Color::MAGENTA, Tracer::Color::RED, Tracer::Color::REVERSE, Tracer::Color::UNDERLINE, Tracer::Color::YELLOW

Instance Attribute Summary

Attributes inherited from Tracer::Base

#header

Instance Method Summary collapse

Methods inherited from Tracer::Base

#colorizable?, #minfo, #out, #pretty_path, #puts, #safe_inspect, #skip?, #skip_internal?, #skip_with_pattern?, #start, #started?, #stop, #stopped?, #to_s

Methods included from Tracer::Color

clear, colorize, #colorize, #colorize_blue, #colorize_cyan, #colorize_magenta

Constructor Details

#initialize(target, var_name, **kw) ⇒ IvarTracer

Returns a new instance of IvarTracer.



6
7
8
9
10
11
# File 'lib/tracer/ivar_tracer.rb', line 6

def initialize(target, var_name, **kw)
  @target = target
  @var_name = var_name
  @original_value = M_INSTANCE_VARIABLE_GET.bind_call(target, var_name)
  super(**kw)
end

Instance Method Details

#descriptionObject



17
18
19
# File 'lib/tracer/ivar_tracer.rb', line 17

def description
  "for #{@var_name} of #{@target} #{super}"
end

#keyObject



13
14
15
# File 'lib/tracer/ivar_tracer.rb', line 13

def key
  [@type, @target, @var_name, @pattern, @into].freeze
end

#setup_tpObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tracer/ivar_tracer.rb', line 21

def setup_tp
  TracePoint.new(:a_return) do |tp|
    next if skip?(tp)

    if tp.self == @target &&
         value = M_INSTANCE_VARIABLE_GET.bind_call(@target, @var_name)
      if tp.event == :c_return
        location = nil
      else
        location = caller_locations(2, 1).first.to_s
        next if location.match?(DIR) || location.match?(/<internal:/)
      end

      depth = caller.size
      call_identifier_str = (tp.defined_class ? minfo(tp) : "block")
      call_identifier_str = colorize_blue(call_identifier_str)
      depth += 1 if tp.event == :c_return
      value = safe_inspect(value)

      if value != @original_value
        out tp,
            "#{call_identifier_str} sets #{colorize_cyan(@var_name)} = #{colorize_magenta(value)}",
            depth: depth - 2 - @depth_offset,
            location: location
      end
    end
  end
end