class SourceRoute::TpResult

what solution is good for summarize attrs that required to be included

Constants

CUSTOM_ATTRS

customized attrs for build html output

INNER_ATTRS

attrs generated from TracePoint.binding

TP_ATTRS

attrs from TracePoint object

Public Class Methods

new(tp_ins) click to toggle source

The tricky part is can't call method on @tp_ins outside trace block finished it could be a security limitation in ruby TracePoint object so collect_required_data can only run once and immediately

# File lib/source_route/tp_result.rb, line 22
def initialize(tp_ins)
  @tp_ins = tp_ins
  collect_required_data
end

Public Instance Methods

==(other) click to toggle source
# File lib/source_route/tp_result.rb, line 43
def ==(other)
  tp_self_refer == other.tp_self_refer and # path == other.path and lineno == other.lineno
    method_id == other.method_id and defined_class == other.defined_class
end
call_event?() click to toggle source
# File lib/source_route/tp_result.rb, line 39
def call_event?
  event == :call
end
collect_required_data() click to toggle source
# File lib/source_route/tp_result.rb, line 101
def collect_required_data
  get_attrs
  get_self_refer

  get_local_or_params_var if SourceRoute.proxy.config.include_local_var
  get_instance_var if SourceRoute.proxy.config.include_instance_var and return_event?
  self
end
found_opposite() click to toggle source
# File lib/source_route/tp_result.rb, line 27
def found_opposite
  @opposite_exist = true
end
get_attrs() click to toggle source
# File lib/source_route/tp_result.rb, line 110
def get_attrs
  GenerateResult.wanted_attributes(@tp_ins.event).each do |key|
    if @tp_ins.respond_to?(key)
      send("#{key}=", @tp_ins.send(key))
    end
  end
end
get_instance_var() click to toggle source
# File lib/source_route/tp_result.rb, line 142
def get_instance_var
  instance_var_hash = {}
  @tp_ins.self.instance_variables.each do |key|
    instance_var_hash[key] = @tp_ins.self.instance_variable_get(key).source_route_display
  end
  self.instance_var = instance_var_hash if instance_var_hash != {}
end
get_local_or_params_var() click to toggle source
# File lib/source_route/tp_result.rb, line 123
def get_local_or_params_var
  local_var_hash = {}
  # Warn: @tp_ins.binding.eval('local_variables') =! @tp_ins.binding.send('local_variables')
  # Params var is complex, see https://alexcastano.com/everything-about-ruby-splats/ for more details
  # so actually here just get local variables which includes params
  @tp_ins.binding.eval('local_variables').each do |v|
    # I need comment out why i need source_route_display
    # must be some strange variables require it
    local_var_hash[v] = @tp_ins.binding.local_variable_get(v).source_route_display
  end
  if local_var_hash != {}
    if call_event?
      self.params_var = local_var_hash
    elsif return_event? # what about other event?
      self.local_var = local_var_hash
    end
  end
end
get_self_refer() click to toggle source
# File lib/source_route/tp_result.rb, line 118
def get_self_refer
  self.tp_self_refer = SourceRoute.proxy.result_builder.tp_self_caches
                       .map(&:__id__).index(@tp_ins.self.__id__)
end
locate_opposite?() click to toggle source
# File lib/source_route/tp_result.rb, line 31
def locate_opposite?
  @opposite_exist
end
matched?() click to toggle source
# File lib/source_route/tp_result.rb, line 48
def matched?
  @matched
end
return_event?() click to toggle source
# File lib/source_route/tp_result.rb, line 35
def return_event?
  event == :return
end
return_tp_assign_call_tp(call_tp) click to toggle source
# File lib/source_route/tp_result.rb, line 52
def return_tp_assign_call_tp(call_tp)
  @matched = true
  call_tp.return_value = return_value
  call_tp.local_var = local_var unless local_var.nil?
  call_tp.instance_var = instance_var unless instance_var.nil?
end
stringify() click to toggle source

todo: this is a mutable method not a good solution. we should use it on the return hash of method to_hash

# File lib/source_route/tp_result.rb, line 83
def stringify
  if GenerateResult.wanted_attributes(event).include?(:defined_class)
    self.defined_class = defined_class.to_s
  end
  if GenerateResult.wanted_attributes(event).include?(:return_value)
    if return_value.nil? or return_value.is_a? Symbol or
      # ActiveRecord::ConnectionAdapters::Column override method ==
      (return_value.is_a? String and return_value == '')
      self.return_value = return_value.inspect
    else
      self.return_value = return_value.to_s
    end
  end
  self.event = event.to_s
  self.method_id = method_id.to_s
  self
end
to_hash() click to toggle source
# File lib/source_route/tp_result.rb, line 59
def to_hash
  stringify
  ret_hash = GenerateResult.wanted_attributes(event).inject({}) do |memo, k|
    memo[k.to_s] = send(k)
    memo
  end

  if SourceRoute.proxy.config.event.include?(:return)
    ret_hash['return_value'] = return_value.nil? ? return_value.inspect : return_value
  end

  (INNER_ATTRS + CUSTOM_ATTRS).each do |k|
    ret_hash[k.to_s] = send(k) if send(k)
  end
  ret_hash['return_value_class'] = ret_hash['return_value'].class.name
  ret_hash['params_var_class'] = ret_hash['params_var'].class.name if ret_hash['params_var']
  ret_hash['local_var_class'] = ret_hash['local_var'].class.name if ret_hash['local_var']
  ret_hash['instance_var_class'] = ret_hash['instance_var'].class.name if ret_hash['instance_var']
  ret_hash
end