Class: SourceRoute::Wrapper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/source_route/wrapper.rb

Constant Summary collapse

TRACE_POINT_METHODS =
[:defined_class, :method_id, :path, :lineno]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWrapper

Returns a new instance of Wrapper.



9
10
11
# File 'lib/source_route/wrapper.rb', line 9

def initialize
  reset
end

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



6
7
8
# File 'lib/source_route/wrapper.rb', line 6

def conditions
  @conditions
end

#output_include_instance_variablesObject

Returns the value of attribute output_include_instance_variables.



7
8
9
# File 'lib/source_route/wrapper.rb', line 7

def output_include_instance_variables
  @output_include_instance_variables
end

#output_include_local_variablesObject

Returns the value of attribute output_include_local_variables.



7
8
9
# File 'lib/source_route/wrapper.rb', line 7

def output_include_local_variables
  @output_include_local_variables
end

#tpObject

Returns the value of attribute tp.



6
7
8
# File 'lib/source_route/wrapper.rb', line 6

def tp
  @tp
end

#tp_attrs_resultsObject

Returns the value of attribute tp_attrs_results.



6
7
8
# File 'lib/source_route/wrapper.rb', line 6

def tp_attrs_results
  @tp_attrs_results
end

Instance Method Details

#events(v) ⇒ Object Also known as: event



26
27
28
# File 'lib/source_route/wrapper.rb', line 26

def events(v)
  @conditions.events = Array(v).map(&:to_sym) unless v.nil?
end

#output_format(data = nil, &block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/source_route/wrapper.rb', line 37

def output_format(data = nil, &block)
  conditions.result_config[:output_format] = if block_given?
                                               block
                                             else
                                               data
                                             end
end

#resetObject

output_format can be console, html



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/source_route/wrapper.rb', line 14

def reset
  @tp.disable if @tp
  @conditions = OpenStruct.new(events: [:call], negative: {}, positive: {},
                               result_config: { output_format: 'none',
                                 selected_attrs: nil,
                                 include_local_var: false,
                                 include_instance_var: false
                               })
  @tp_attrs_results = []
  self
end

#selected_attrs(*attr) ⇒ Object



45
46
47
# File 'lib/source_route/wrapper.rb', line 45

def selected_attrs(*attr)
  conditions.result_config[:selected_attrs] = Array(attr)
end

#set_result_config(value) ⇒ Object



31
32
33
34
35
# File 'lib/source_route/wrapper.rb', line 31

def set_result_config(value)
  unless value.is_a? Hash
    conditions.result_config = value
  end
end

#traceObject



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
# File 'lib/source_route/wrapper.rb', line 69

def trace
  # dont wanna init it in tp block, cause tp block could run thousands of time in one cycle trace
  tp_result = TpResult.new(self)

  track = TracePoint.new *conditions.events do |tp|
    negative_break = conditions.negative.any? do |method_key, value|
      tp.send(method_key).nature_value =~ Regexp.new(value)
    end
    next if negative_break
    positive_break = conditions.positive.any? do |method_key, value|
      tp.send(method_key).nature_value !~ Regexp.new(value)
    end
    next if positive_break

    unless conditions[:result_config][:output_format].is_a? Proc
      ret_data = tp_result.build(tp)
      tp_attrs_results.push(ret_data)
    end

    tp_result.output(tp)
  end
  track.enable
  self.tp = track
  track
end