Class: TappingDevice::Trackers::InitializationTracker

Inherits:
TappingDevice
  • Object
show all
Defined in:
lib/tapping_device/trackers/initialization_tracker.rb

Constant Summary

Constants inherited from TappingDevice

CALLER_START_POINT, C_CALLER_START_POINT, VERSION

Instance Attribute Summary

Attributes inherited from TappingDevice

#calls, #options, #target, #trace_point

Instance Method Summary collapse

Methods inherited from TappingDevice

config, #create_child_device, #descendants, #root_device, #set_block, #stop!, #stop_when, #with

Methods included from Manageable

#delete_device, #devices, #reset!, #stop_all!, #suspend_new, #suspend_new!

Methods included from Output::Helpers

#and_output, #and_print, #and_write

Constructor Details

#initialize(options = {}, &block) ⇒ InitializationTracker

Returns a new instance of InitializationTracker.



4
5
6
7
8
9
10
# File 'lib/tapping_device/trackers/initialization_tracker.rb', line 4

def initialize(options = {}, &block)
  super
  event_type = @options[:event_type]
  # if a class doesn't override the 'initialize' method
  # Class.new will only trigger c_return or c_call
  @options[:event_type] = [event_type, "c_#{event_type}"]
end

Instance Method Details

#build_payload(tp:, filepath:, line_number:) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/tapping_device/trackers/initialization_tracker.rb', line 18

def build_payload(tp:, filepath:, line_number:)
  payload = super

  return payload if @is_active_record_model

  payload.return_value = payload.receiver
  payload.receiver = target
  payload
end

#filter_condition_satisfied?(tp) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tapping_device/trackers/initialization_tracker.rb', line 32

def filter_condition_satisfied?(tp)
  receiver = tp.self
  method_name = tp.callee_id

  if @is_active_record_model
    # ActiveRecord redefines model classes' .new method,
    # so instead of calling Model#initialize, it'll actually call Model.new
    # see https://github.com/rails/rails/blob/master/activerecord/lib/active_record/inheritance.rb#L50
    method_name == :new &&
      receiver.is_a?(Class) &&
      # this checks if the model class is the target class or a subclass of it
      receiver.ancestors.include?(target) &&
      # Model.new triggers both c_return and return events. so we should only return in 1 type of the events
      # otherwise the callback will be triggered twice
      tp.event == :return
  else
    method_name == :initialize && receiver.is_a?(target)
  end
end

#track(object) ⇒ Object



12
13
14
15
16
# File 'lib/tapping_device/trackers/initialization_tracker.rb', line 12

def track(object)
  super
  @is_active_record_model = defined?(ActiveRecord) && target.ancestors.include?(ActiveRecord::Base)
  self
end

#validate_target!Object

Raises:



28
29
30
# File 'lib/tapping_device/trackers/initialization_tracker.rb', line 28

def validate_target!
  raise NotAClassError.new(target) unless target.is_a?(Class)
end