Class: MethodTracer::Tracer
- Inherits:
-
Object
- Object
- MethodTracer::Tracer
- Defined in:
- lib/method_tracer/tracer.rb
Instance Method Summary collapse
- #enable ⇒ Object
-
#initialize(path:, name: nil) ⇒ Tracer
constructor
A new instance of Tracer.
- #method_is_defined_in_target_path(candidate_class, method_id) ⇒ Object
- #method_is_interesting?(candidate_class, method_id) ⇒ Boolean
- #outfile ⇒ Object
- #record_call_if_interesting(tp) ⇒ Object
Constructor Details
#initialize(path:, name: nil) ⇒ Tracer
Returns a new instance of Tracer.
5 6 7 8 |
# File 'lib/method_tracer/tracer.rb', line 5 def initialize(path:, name: nil) @target_path = path @target_name = name end |
Instance Method Details
#enable ⇒ Object
10 11 12 13 14 |
# File 'lib/method_tracer/tracer.rb', line 10 def enable @tracer = TracePoint.trace(:call) do |tp| record_call_if_interesting(tp) end end |
#method_is_defined_in_target_path(candidate_class, method_id) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/method_tracer/tracer.rb', line 55 def method_is_defined_in_target_path(candidate_class, method_id) begin location = Where.is(candidate_class, method_id) rescue NameError return false end location[:file].start_with?(@target_path) end |
#method_is_interesting?(candidate_class, method_id) ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/method_tracer/tracer.rb', line 40 def method_is_interesting?(candidate_class, method_id) candidate_name = if candidate_class.instance_of?(Class) candidate_class.name else candidate_class.class.name end # short circuit if possible for speed return false if !@target_name.nil? && !candidate_name.nil? && !candidate_name.include?(@target_name) method_is_defined_in_target_path(candidate_class, method_id) end |
#outfile ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/method_tracer/tracer.rb', line 27 def outfile @outfile ||= begin output_file = Config.output_file if output_file.instance_of?(IO) || output_file.instance_of?(StringIO) output_file elsif output_file.instance_of?(String) File.open(output_file, 'a') else raise "Unhandled output_file type: #{output_file}" end end end |
#record_call_if_interesting(tp) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/method_tracer/tracer.rb', line 16 def record_call_if_interesting(tp) return unless method_is_interesting?(tp.defined_class, tp.method_id) locations = caller_locations.select { |loc| loc.path.start_with?(Config.app_path) } return if locations.empty? outfile.write "#{tp.defined_class} :#{tp.method_id} " outfile.write locations.map { |loc| "#{loc.path}:#{loc.lineno}" }.join('; ') outfile.write "\n" end |