Class: SourceRoute::TpResult
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
[:order_id, :parent_ids, :direct_child_order_ids, :has_return_value, :parent_length, :tp_self_refer].freeze
Instance Method Summary collapse
- #==(other) ⇒ Object
- #call_event? ⇒ Boolean
- #collect_required_data ⇒ Object
- #found_opposite ⇒ Object
-
#get_attrs ⇒ Object
Becare.
- #get_instance_var ⇒ Object
- #get_local_or_params_var ⇒ Object
-
#get_self_refer ⇒ Object
def get_additional_attributes [:order_id, :parent_ids, :direct_child_order_ids, :parent_length].each do |k| @ret_data = send(k) unless send(k).nil? end end.
-
#initialize(tp_ins) ⇒ TpResult
constructor
The tricky part is cant call @core after trace block finished.
- #locate_opposite? ⇒ Boolean
- #matched? ⇒ Boolean
- #return_event? ⇒ Boolean
- #return_tp_assign_call_tp(call_tp) ⇒ Object
-
#stringify ⇒ Object
this is a mutable method not a good solution.
-
#to_hash ⇒ Object
to_hash why we need wrapper? it’s nonsense.
Constructor Details
#initialize(tp_ins) ⇒ TpResult
The tricky part is cant call @core after trace block finished
23 24 25 26 |
# File 'lib/source_route/tp_result.rb', line 23 def initialize(tp_ins) @tp_ins = tp_ins # it only workable in TracePoint block collect_required_data # so this method can only called once end |
Instance Method Details
#==(other) ⇒ Object
44 45 46 47 |
# File 'lib/source_route/tp_result.rb', line 44 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
40 41 42 |
# File 'lib/source_route/tp_result.rb', line 40 def call_event? event == :call end |
#collect_required_data ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/source_route/tp_result.rb', line 108 def collect_required_data get_attrs get_self_refer get_local_or_params_var if SourceRoute.wrapper.condition.result_config[:include_local_var] get_instance_var if SourceRoute.wrapper.condition.result_config[:include_instance_var] and return_event? self end |
#found_opposite ⇒ Object
28 29 30 |
# File 'lib/source_route/tp_result.rb', line 28 def found_opposite @opposite_exist = true end |
#get_attrs ⇒ Object
Becare. we cal @tp_ins.event here but in stringify method we jsut call event
119 120 121 122 123 124 125 126 |
# File 'lib/source_route/tp_result.rb', line 119 def get_attrs attrs_data = 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 ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/source_route/tp_result.rb', line 156 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 ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/source_route/tp_result.rb', line 139 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_refer ⇒ Object
def get_additional_attributes
[:order_id, :parent_ids, :direct_child_order_ids, :parent_length].each do |k|
@ret_data[k] = send(k) unless send(k).nil?
end
end
134 135 136 137 |
# File 'lib/source_route/tp_result.rb', line 134 def get_self_refer self.tp_self_refer = SourceRoute.wrapper.result_builder.tp_self_caches .map(&:__id__).index(@tp_ins.self.__id__) end |
#locate_opposite? ⇒ Boolean
32 33 34 |
# File 'lib/source_route/tp_result.rb', line 32 def locate_opposite? @opposite_exist end |
#matched? ⇒ Boolean
49 50 51 |
# File 'lib/source_route/tp_result.rb', line 49 def matched? @matched end |
#return_event? ⇒ Boolean
36 37 38 |
# File 'lib/source_route/tp_result.rb', line 36 def return_event? event == :return end |
#return_tp_assign_call_tp(call_tp) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/source_route/tp_result.rb', line 53 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 ⇒ Object
this is a mutable method not a good solution. we should use it on the return hash of method to_hash
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/source_route/tp_result.rb', line 90 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 ⇒ Object
to_hash why we need wrapper? it’s nonsense
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/source_route/tp_result.rb', line 63 def to_hash stringify ret_hash = GenerateResult.wanted_attributes(event).inject({}) do |memo, k| memo[k.to_s] = send(k) memo end if SourceRoute.wrapper.condition.events.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 |