trace_location 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2194441a9de2b0331c15d48e202e61e05d208703821e62b7d53db4b0baf1e958
4
- data.tar.gz: 87e55afcd03311a0735fecd7a2d6f37865c2bd06df25f60805e57aec0b0f16e4
3
+ metadata.gz: 17ca486d486f5139ac8b32b55092baa1d9ff2d3c8845c3021d9a9314d7d5a177
4
+ data.tar.gz: f1f5ccba6018672a27d52d5927f73fb6ce9f63a32a14d2075e1fc96da7f158ee
5
5
  SHA512:
6
- metadata.gz: eabe3716b3a56ff70aa15a25887cbbfd056953ace2eb1e4c7e532041ca245b160d7d8c5b6e52ad8ff294747d6937c3841fa911d82150770ff45d22d7efa2bc54
7
- data.tar.gz: 631ee8e05dbbf7aeb65c35ec955f8cd56e6f1931fa34e979109f9c4d90dcb5e11b0755f5cc6495df42271dcc0ef047b29040adc57e8a142ec9486a4343a07b6f
6
+ metadata.gz: 92af182afecc84fa9c3f41bcee7ed928e5a90e2427397dd1aaf7786d67c29e98baf203f615bcddcf6e8a17bb3a0ffe04495c20bfcfbcb7f402c17ee0edede6fe
7
+ data.tar.gz: 65153552b73e1ed496f660707344488a1179a2018cae5c41b73d6847d227b7a19ce9acf9a9f907edd4503f5a669ba6e704237a906b9e25f1bcb37223b7024ac4
@@ -1,9 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trace_location (0.9.1)
4
+ trace_location (0.9.2)
5
5
  binding_of_caller
6
- pry
7
6
 
8
7
  GEM
9
8
  remote: https://rubygems.org/
@@ -11,17 +10,12 @@ GEM
11
10
  ast (2.4.0)
12
11
  binding_of_caller (0.8.0)
13
12
  debug_inspector (>= 0.0.1)
14
- coderay (1.1.2)
15
13
  debug_inspector (0.0.3)
16
14
  diff-lcs (1.3)
17
15
  jaro_winkler (1.5.2)
18
- method_source (0.9.2)
19
16
  parallel (1.17.0)
20
17
  parser (2.6.3.0)
21
18
  ast (~> 2.4.0)
22
- pry (0.12.2)
23
- coderay (~> 1.1.0)
24
- method_source (~> 0.9.0)
25
19
  rainbow (3.0.0)
26
20
  rake (12.3.2)
27
21
  rspec (3.8.0)
@@ -7,55 +7,78 @@ module TraceLocation
7
7
  require_relative 'event'
8
8
  Result = Struct.new(:events, :return_value)
9
9
 
10
- def self.collect(match:, ignore:, &block)
11
- events = []
12
- hierarchy = 0
13
- id = 0
14
- cache = {}
15
-
16
- tracer = TracePoint.new(:call, :return) do |trace_point|
17
- next if match && !trace_point.path.to_s.match?(/#{match}/)
18
- next if ignore && trace_point.path.to_s.match?(/#{ignore}/)
19
-
20
- id += 1
21
- caller_path, caller_lineno = trace_point.binding.of_caller(2).source_location
22
- location_cache_key = "#{caller_path}:#{caller_lineno}"
23
-
24
- case trace_point.event
25
- when :call
26
- cache[location_cache_key] = hierarchy
27
-
28
- events << Event.new(
29
- id: id,
30
- event: trace_point.event,
31
- path: trace_point.path,
32
- lineno: trace_point.lineno,
33
- caller_path: caller_path,
34
- caller_lineno: caller_lineno,
35
- method_id: trace_point.method_id,
36
- defined_class: trace_point.defined_class,
37
- hierarchy: hierarchy
38
- )
39
-
40
- hierarchy += 1
41
- when :return
42
- hierarchy = cache[location_cache_key] || hierarchy
43
-
44
- events << Event.new(
45
- id: id,
46
- event: trace_point.event,
47
- path: trace_point.path,
48
- lineno: trace_point.lineno,
49
- caller_path: caller_path,
50
- caller_lineno: caller_lineno,
51
- method_id: trace_point.method_id,
52
- defined_class: trace_point.defined_class,
53
- hierarchy: hierarchy
54
- )
10
+ class << self
11
+ def collect(match:, ignore:, &block)
12
+ events = []
13
+ hierarchy = 0
14
+ id = 0
15
+ cache = {}
16
+
17
+ tracer = TracePoint.new(:call, :return) do |trace_point|
18
+ next if match && !trace_point.path.to_s.match?(/#{match}/)
19
+ next if ignore && trace_point.path.to_s.match?(/#{ignore}/)
20
+
21
+ id += 1
22
+ caller_path, caller_lineno = trace_point.binding.of_caller(2).source_location
23
+ location_cache_key = "#{caller_path}:#{caller_lineno}"
24
+
25
+ mes = extract_method_from(trace_point)
26
+
27
+ case trace_point.event
28
+ when :call
29
+ cache[location_cache_key] = hierarchy
30
+
31
+ events << Event.new(
32
+ id: id,
33
+ event: trace_point.event,
34
+ path: trace_point.path,
35
+ lineno: trace_point.lineno,
36
+ caller_path: caller_path,
37
+ caller_lineno: caller_lineno,
38
+ owner: mes.owner,
39
+ name: mes.name,
40
+ source: remove_indent(mes.source),
41
+ hierarchy: hierarchy,
42
+ is_module: trace_point.self.is_a?(Module)
43
+ )
44
+
45
+ hierarchy += 1
46
+ when :return
47
+ hierarchy = cache[location_cache_key] || hierarchy
48
+
49
+ events << Event.new(
50
+ id: id,
51
+ event: trace_point.event,
52
+ path: trace_point.path,
53
+ lineno: trace_point.lineno,
54
+ caller_path: caller_path,
55
+ caller_lineno: caller_lineno,
56
+ owner: mes.owner,
57
+ name: mes.name,
58
+ source: remove_indent(mes.source),
59
+ hierarchy: hierarchy,
60
+ is_module: trace_point.self.is_a?(Module)
61
+ )
62
+ end
55
63
  end
64
+ return_value = tracer.enable { block.call }
65
+ Result.new(events, return_value)
66
+ end
67
+
68
+ private
69
+
70
+ def extract_method_from(trace_point)
71
+ if trace_point.self.is_a?(Module)
72
+ ::Module.instance_method(:method).bind(trace_point.self).call(trace_point.method_id)
73
+ else
74
+ ::Kernel.instance_method(:method).bind(trace_point.self).call(trace_point.method_id)
75
+ end
76
+ end
77
+
78
+ def remove_indent(source)
79
+ indent = source.split("\n").first.match(/\A(\s+).+\z/)[1]
80
+ source.split("\n").map { |line| line.gsub(/\A#{indent}/, '') }.join("\n")
56
81
  end
57
- return_value = tracer.enable { block.call }
58
- Result.new(events, return_value)
59
82
  end
60
83
  end
61
84
  end
@@ -3,18 +3,20 @@
3
3
  module TraceLocation
4
4
  class Event # :nodoc:
5
5
  CLASS_FORMAT = /\A#<(?:Class|refinement)\:([A-Za-z0-9\:]+).*>\z/.freeze
6
- attr_reader :id, :event, :path, :lineno, :caller_path, :caller_lineno, :method_id, :defined_class, :hierarchy
6
+ attr_reader :id, :event, :path, :lineno, :caller_path, :caller_lineno, :owner, :name, :source, :hierarchy, :is_module
7
7
 
8
- def initialize(id:, event:, path:, lineno:, caller_path:, caller_lineno:, method_id:, defined_class:, hierarchy:)
8
+ def initialize(id:, event:, path:, lineno:, caller_path:, caller_lineno:, owner:, name:, source:, hierarchy:, is_module:)
9
9
  @id = id
10
10
  @event = event
11
11
  @path = path
12
12
  @lineno = lineno
13
13
  @caller_path = caller_path
14
14
  @caller_lineno = caller_lineno
15
- @method_id = method_id
16
- @defined_class = defined_class
15
+ @owner = owner
16
+ @name = name
17
+ @source = source
17
18
  @hierarchy = hierarchy.to_i
19
+ @is_module = is_module
18
20
  end
19
21
 
20
22
  def valid?
@@ -33,14 +35,10 @@ module TraceLocation
33
35
  event == :return
34
36
  end
35
37
 
36
- def method_str
37
- match = defined_class.to_s.match(CLASS_FORMAT)
38
-
39
- if match
40
- "#{match[1]}.#{method_id}"
41
- else
42
- "#{defined_class}##{method_id}"
43
- end
38
+ def owner_with_name
39
+ match = owner.to_s.match(CLASS_FORMAT)
40
+ separator = is_module ? '.' : '#'
41
+ match ? "#{match[1]}#{separator}#{name}" : "#{owner}#{separator}#{name}"
44
42
  end
45
43
  end
46
44
  end
@@ -5,7 +5,7 @@ require 'csv'
5
5
  module TraceLocation
6
6
  module Generator
7
7
  class Csv < Base # :nodoc:
8
- ATTRIBUTES = %w[id event path lineno caller_path caller_lineno method_str hierarchy].freeze
8
+ ATTRIBUTES = %w[id event path lineno caller_path caller_lineno owner_with_name hierarchy].freeze
9
9
 
10
10
  def initialize(events, return_value, options)
11
11
  super
@@ -51,7 +51,7 @@ module TraceLocation
51
51
  event = EVENTS[e.event]
52
52
  path = e.path.to_s.gsub(%r{#{gems_dir}/}, '')
53
53
 
54
- %(#{indent}#{event} #{path}:#{e.lineno} [#{e.method_str}])
54
+ %(#{indent}#{event} #{path}:#{e.lineno} [#{e.owner_with_name}])
55
55
  end
56
56
  end
57
57
 
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pry'
4
-
5
3
  module TraceLocation
6
4
  module Generator
7
5
  class Markdown < Base # :nodoc:
@@ -36,25 +34,20 @@ module TraceLocation
36
34
  MARKDOWN
37
35
 
38
36
  events.select(&:call?).each do |e|
39
- pm = Pry::Method.from_str(e.method_str)
40
- next if pm.nil?
41
-
42
37
  path = e.path.to_s.gsub(%r{#{gems_dir}/}, '')
43
38
  caller_path = e.caller_path.to_s.gsub(%r{#{gems_dir}/}, '')
44
39
  io.write <<~MARKDOWN
45
40
  <details open>
46
41
  <summary>#{path}:#{e.lineno}</summary>
47
42
 
48
- ##### #{pm.name_with_owner}
43
+ ##### #{e.owner_with_name}
49
44
 
50
- ```#{pm.source_type}
51
- #{pm.source.chomp}
45
+ ```ruby
46
+ #{e.source}
52
47
  # called from #{caller_path}:#{e.caller_lineno}
53
48
  ```
54
49
  </details>
55
50
  MARKDOWN
56
- rescue StandardError
57
- next
58
51
  end
59
52
  end
60
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TraceLocation
4
- VERSION = '0.9.1'
4
+ VERSION = '0.9.2'
5
5
  end
@@ -24,7 +24,6 @@ Gem::Specification.new do |s|
24
24
  s.required_ruby_version = '>= 2.5.0'
25
25
 
26
26
  s.add_dependency 'binding_of_caller'
27
- s.add_dependency 'pry'
28
27
 
29
28
  s.add_development_dependency 'bundler'
30
29
  s.add_development_dependency 'rake'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trace_location
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshiyuki Hirano
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-04 00:00:00.000000000 Z
11
+ date: 2019-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: binding_of_caller
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: pry
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: bundler
43
29
  requirement: !ruby/object:Gem::Requirement