Class: SourceRoute::TpResult

Inherits:
Object
  • Object
show all
Defined in:
lib/source_route/tp_result.rb

Overview

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

Constant Summary collapse

TP_ATTRS =

attrs from TracePoint object

[:event, :defined_class, :event, :lineno, :method_id,
:path, :raised_exception, :return_value].freeze
INNER_ATTRS =

attrs generated from TracePoint.binding

[:local_var, :instance_var, :params_var].freeze
CUSTOM_ATTRS =

customized attrs for build html output

[:order_id, :parent_ids, :direct_child_order_ids,
:has_return_value, :parent_length, :tp_self_refer].freeze

Instance Method Summary collapse

Constructor Details

#initialize(tp_ins) ⇒ TpResult

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



22
23
24
25
# File 'lib/source_route/tp_result.rb', line 22

def initialize(tp_ins)
  @tp_ins = tp_ins
  collect_required_data
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
46
# File 'lib/source_route/tp_result.rb', line 43

def ==(other)
  tp_self_refer == other.tp_self_refer and method_id == other.method_id and
    defined_class == other.defined_class
end

#call_event?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/source_route/tp_result.rb', line 39

def call_event?
  event == :call
end

#collect_required_dataObject



95
96
97
98
99
100
101
102
# File 'lib/source_route/tp_result.rb', line 95

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_oppositeObject



27
28
29
# File 'lib/source_route/tp_result.rb', line 27

def found_opposite
  @opposite_exist = true
end

#get_attrsObject



104
105
106
107
108
109
110
# File 'lib/source_route/tp_result.rb', line 104

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_varObject



134
135
136
137
138
139
140
# File 'lib/source_route/tp_result.rb', line 134

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_varObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/source_route/tp_result.rb', line 117

def get_local_or_params_var
  local_var_hash = {}
  # Warn: @tp_ins.binding.eval('local_variables') =! @tp_ins.binding.send('local_variables')
  @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_referObject



112
113
114
115
# File 'lib/source_route/tp_result.rb', line 112

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?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/source_route/tp_result.rb', line 31

def locate_opposite?
  @opposite_exist
end

#matched?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/source_route/tp_result.rb', line 48

def matched?
  @matched
end

#return_event?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/source_route/tp_result.rb', line 35

def return_event?
  event == :return
end

#return_tp_assign_call_tp(call_tp) ⇒ Object



52
53
54
55
56
57
# 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

#stringifyObject

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/source_route/tp_result.rb', line 77

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_hashObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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
end