Class: ObjectTracer

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

Constant Summary collapse

PRIMITIVE_METHOD_SOURCES =
[Module, Class, Object, Kernel]

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 collapse

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 = nil, target_id: nil, target_label: nil, **kw) ⇒ ObjectTracer

Returns a new instance of ObjectTracer.



8
9
10
11
12
13
14
15
16
17
# File 'lib/tracer/object_tracer.rb', line 8

def initialize(target = nil, target_id: nil, target_label: nil, **kw)
  unless target || target_id
    raise ArgumentError, "target or target_id is required"
  end

  @target_id = target_id || M_OBJECT_ID.bind_call(target)
  @target_label =
    (target ? safe_inspect(target) : target_label || "<unlabelled>")
  super(**kw)
end

Instance Attribute Details

#target_idObject (readonly)

Returns the value of attribute target_id.



6
7
8
# File 'lib/tracer/object_tracer.rb', line 6

def target_id
  @target_id
end

#target_labelObject (readonly)

Returns the value of attribute target_label.



6
7
8
# File 'lib/tracer/object_tracer.rb', line 6

def target_label
  @target_label
end

Instance Method Details

#colorized_target_labelObject



27
28
29
# File 'lib/tracer/object_tracer.rb', line 27

def colorized_target_label
  colorize_magenta(@target_label)
end

#descriptionObject



23
24
25
# File 'lib/tracer/object_tracer.rb', line 23

def description
  "for #{@target_label} #{super}"
end

#keyObject



19
20
21
# File 'lib/tracer/object_tracer.rb', line 19

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

#setup_tpObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tracer/object_tracer.rb', line 33

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

    if M_OBJECT_ID.bind_call(tp.self) == @target_id
      if PRIMITIVE_METHOD_SOURCES.any? { |klass| klass == tp.defined_class }
        next
      end

      internal_depth = 2
      klass = tp.defined_class
      method = tp.method_id
      method_info =
        method_info =
          if klass
            if klass.singleton_class?
              if M_IS_A.bind_call(tp.self, Class)
                ".#{method} (#{klass}.#{method})"
              else
                ".#{method}"
              end
            else
              "##{method} (#{klass}##{method})"
            end
          else
            if method
              "##{method} (<unknown>##{method})"
            else
              "<eval or exec with &block>"
            end
          end

      out tp,
          " #{colorized_target_label} receives #{colorize_blue(method_info)}",
          location: caller_locations(internal_depth, 1).first,
          depth: caller.size - internal_depth - @depth_offset
    elsif !tp.parameters.empty?
      b = tp.binding
      method_info = colorize_blue(minfo(tp))

      tp.parameters.each do |type, name|
        next unless name

        colorized_name = colorize_cyan(name)

        case type
        when :req, :opt, :key, :keyreq
          if M_OBJECT_ID.bind_call(b.local_variable_get(name)) == @target_id
            internal_depth = 4
            out tp,
                " #{colorized_target_label} is used as a parameter #{colorized_name} of #{method_info}",
                location: caller_locations(internal_depth, 1).first,
                depth: caller.size - internal_depth - @depth_offset
          end
        when :rest
          next if name == :"*"

          internal_depth = 6
          ary = b.local_variable_get(name)
          ary.each do |e|
            if M_OBJECT_ID.bind_call(e) == @target_id
              out tp,
                  " #{colorized_target_label} is used as a parameter in #{colorized_name} of #{method_info}",
                  location: caller_locations(internal_depth, 1).first,
                  depth: caller.size - internal_depth - @depth_offset
            end
          end
        when :keyrest
          next if name == :"**"
          internal_depth = 6
          h = b.local_variable_get(name)
          h.each do |k, e|
            if M_OBJECT_ID.bind_call(e) == @target_id
              out tp,
                  " #{colorized_target_label} is used as a parameter in #{colorized_name} of #{method_info}",
                  location: caller_locations(internal_depth, 1).first,
                  depth: caller.size - internal_depth - @depth_offset
            end
          end
        end
      end
    end
  end
end