trace_location 0.4.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac60bad7314ff13ccd86da5e6b88bff9dd50afedb445a3afabee76495aee49fb
4
- data.tar.gz: 911bbeafa5bdb0d29d47e99fdb1cee89b55abc8b9d44f4157c5b9833e5dc093c
3
+ metadata.gz: b7f8eaee17fe797de0ea3da6917dabcaa764bf9037a5748905061775edb9d3df
4
+ data.tar.gz: c54da8e56e1ab1d10eaab5ca1c946914ed79b9b371a6705f7854a2f96165ed91
5
5
  SHA512:
6
- metadata.gz: 211e0891220cb42c8def533b18ca2baeb55fd91c88c847d861482e5a80cae0feba5d5c292ec4b537fcf6173525c49f337bd46ce93f12922a9b6772a92a73227a
7
- data.tar.gz: 7fa374686f1131f66e1046469fae708f643a0a6be6040f9773da60c2777e0a8a03c51e8962300d4ffe83d1fe1afd5dd4879fd2e4490db4b134e25185e65770d6
6
+ metadata.gz: afc12795bbe5f3fcd185ae87163c978484bc4be5bc793ed6df4da83dd8885ccac1df7c3cd2e059520d41577ec95bcda8e3b49ba426abe7daf1a03894ce833405
7
+ data.tar.gz: 6b680efb7dc27b7dc804df6ef55cdb07f5a78c4f4e0fb42f6733d125f6d04fa07ce2962c65140c502953038c94a6789c827e8c151c7f8713c60051d85b5b3672
data/Gemfile.lock CHANGED
@@ -1,19 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trace_location (0.4.0)
4
+ trace_location (0.9.0)
5
+ binding_of_caller
6
+ pry
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
8
10
  specs:
9
11
  ast (2.4.0)
12
+ binding_of_caller (0.8.0)
13
+ debug_inspector (>= 0.0.1)
14
+ coderay (1.1.2)
15
+ debug_inspector (0.0.3)
10
16
  diff-lcs (1.3)
11
17
  jaro_winkler (1.5.2)
18
+ method_source (0.9.2)
12
19
  parallel (1.17.0)
13
20
  parser (2.6.3.0)
14
21
  ast (~> 2.4.0)
22
+ pry (0.12.2)
23
+ coderay (~> 1.1.0)
24
+ method_source (~> 0.9.0)
15
25
  rainbow (3.0.0)
16
- rake (10.5.0)
26
+ rake (12.3.2)
17
27
  rspec (3.8.0)
18
28
  rspec-core (~> 3.8.0)
19
29
  rspec-expectations (~> 3.8.0)
@@ -27,23 +37,23 @@ GEM
27
37
  diff-lcs (>= 1.2.0, < 2.0)
28
38
  rspec-support (~> 3.8.0)
29
39
  rspec-support (3.8.0)
30
- rubocop (0.68.1)
40
+ rubocop (0.71.0)
31
41
  jaro_winkler (~> 1.5.1)
32
42
  parallel (~> 1.10)
33
- parser (>= 2.5, != 2.5.1.1)
43
+ parser (>= 2.6)
34
44
  rainbow (>= 2.2.2, < 4.0)
35
45
  ruby-progressbar (~> 1.7)
36
- unicode-display_width (>= 1.4.0, < 1.6)
37
- ruby-progressbar (1.10.0)
38
- unicode-display_width (1.5.0)
46
+ unicode-display_width (>= 1.4.0, < 1.7)
47
+ ruby-progressbar (1.10.1)
48
+ unicode-display_width (1.6.0)
39
49
 
40
50
  PLATFORMS
41
51
  ruby
42
52
 
43
53
  DEPENDENCIES
44
- bundler (~> 2.0)
45
- rake (~> 10.0)
46
- rspec (~> 3.0)
54
+ bundler
55
+ rake
56
+ rspec
47
57
  rubocop
48
58
  trace_location!
49
59
 
data/README.md CHANGED
@@ -22,14 +22,307 @@ Or install it yourself as:
22
22
 
23
23
  You just surround the code which you want to track the process.
24
24
 
25
- ### Example 01. Track the lifecycle of Rails application
25
+ ### Example 01. Track the validation process of Active Record
26
+
27
+ ```
28
+ % bin/rails c
29
+
30
+ irb(main):001:0> book = Book.new(title: "My Book Title")
31
+ irb(main):002:0> TraceLocation.trace(ignore: /active_support/) { book.validate }
32
+ Created at /path/to/sampleapp/log/trace_location-2019060401061559579741.md
33
+ => true
34
+ ```
35
+
36
+ Then you can get a markdown file like this:
37
+
38
+ --
39
+
40
+ Generated by [trace_location](https://github.com/yhirano55/trace_location) at 2019-06-04 01:35:41 +0900
41
+
42
+ <details open>
43
+ <summary>activerecord-5.2.3/lib/active_record/validations.rb:65</summary>
44
+
45
+ ##### ActiveRecord::Validations#valid?
46
+ ```ruby
47
+ def valid?(context = nil)
48
+ context ||= default_validation_context
49
+ output = super(context)
50
+ errors.empty? && output
51
+ end
52
+ # called from (irb):2
53
+ ```
54
+
55
+ </details>
56
+ <details open>
57
+ <summary>activerecord-5.2.3/lib/active_record/validations.rb:75</summary>
58
+
59
+ ##### ActiveRecord::Validations#default_validation_context
60
+ ```ruby
61
+ def default_validation_context
62
+ new_record? ? :create : :update
63
+ end
64
+ # called from activerecord-5.2.3/lib/active_record/validations.rb:66
65
+ ```
66
+
67
+ </details>
68
+ <details open>
69
+ <summary>activerecord-5.2.3/lib/active_record/persistence.rb:231</summary>
70
+
71
+ ##### ActiveRecord::Persistence#new_record?
72
+ ```ruby
73
+ def new_record?
74
+ sync_with_transaction_state
75
+ @new_record
76
+ end
77
+ # called from activerecord-5.2.3/lib/active_record/validations.rb:76
78
+ ```
79
+
80
+ </details>
81
+ <details open>
82
+ <summary>activerecord-5.2.3/lib/active_record/transactions.rb:490</summary>
83
+
84
+ ##### ActiveRecord::Transactions#sync_with_transaction_state
85
+ ```ruby
86
+ def sync_with_transaction_state
87
+ update_attributes_from_transaction_state(@transaction_state)
88
+ end
89
+ # called from activerecord-5.2.3/lib/active_record/persistence.rb:232
90
+ ```
91
+
92
+ </details>
93
+ <details open>
94
+ <summary>activerecord-5.2.3/lib/active_record/transactions.rb:494</summary>
95
+
96
+ ##### ActiveRecord::Transactions#update_attributes_from_transaction_state
97
+ ```ruby
98
+ def update_attributes_from_transaction_state(transaction_state)
99
+ if transaction_state && transaction_state.finalized?
100
+ restore_transaction_record_state(transaction_state.fully_rolledback?) if transaction_state.rolledback?
101
+ force_clear_transaction_record_state if transaction_state.fully_committed?
102
+ clear_transaction_record_state if transaction_state.fully_completed?
103
+ end
104
+ end
105
+ # called from activerecord-5.2.3/lib/active_record/transactions.rb:491
106
+ ```
107
+
108
+ </details>
109
+ <details open>
110
+ <summary>activemodel-5.2.3/lib/active_model/validations.rb:336</summary>
111
+
112
+ ##### ActiveModel::Validations#valid?
113
+ ```ruby
114
+ def valid?(context = nil)
115
+ current_context, self.validation_context = validation_context, context
116
+ errors.clear
117
+ run_validations!
118
+ ensure
119
+ self.validation_context = current_context
120
+ end
121
+ # called from activerecord-5.2.3/lib/active_record/validations.rb:67
122
+ ```
123
+
124
+ </details>
125
+ <details open>
126
+ <summary>activemodel-5.2.3/lib/active_model/validations.rb:303</summary>
127
+
128
+ ##### ActiveModel::Validations#errors
129
+ ```ruby
130
+ def errors
131
+ @errors ||= Errors.new(self)
132
+ end
133
+ # called from activemodel-5.2.3/lib/active_model/validations.rb:338
134
+ ```
135
+
136
+ </details>
137
+ <details open>
138
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:74</summary>
139
+
140
+ ##### ActiveModel::Errors#initialize
141
+ ```ruby
142
+ def initialize(base)
143
+ @base = base
144
+ @messages = apply_default_array({})
145
+ @details = apply_default_array({})
146
+ end
147
+ # called from activemodel-5.2.3/lib/active_model/validations.rb:304
148
+ ```
149
+
150
+ </details>
151
+ <details open>
152
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:470</summary>
153
+
154
+ ##### ActiveModel::Errors#apply_default_array
155
+ ```ruby
156
+ def apply_default_array(hash)
157
+ hash.default_proc = proc { |h, key| h[key] = [] }
158
+ hash
159
+ end
160
+ # called from activemodel-5.2.3/lib/active_model/errors.rb:76
161
+ ```
162
+
163
+ </details>
164
+ <details open>
165
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:470</summary>
166
+
167
+ ##### ActiveModel::Errors#apply_default_array
168
+ ```ruby
169
+ def apply_default_array(hash)
170
+ hash.default_proc = proc { |h, key| h[key] = [] }
171
+ hash
172
+ end
173
+ # called from activemodel-5.2.3/lib/active_model/errors.rb:77
174
+ ```
175
+
176
+ </details>
177
+ <details open>
178
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:115</summary>
179
+
180
+ ##### ActiveModel::Errors#clear
181
+ ```ruby
182
+ def clear
183
+ messages.clear
184
+ details.clear
185
+ end
186
+ # called from activemodel-5.2.3/lib/active_model/validations.rb:338
187
+ ```
188
+
189
+ </details>
190
+ <details open>
191
+ <summary>activemodel-5.2.3/lib/active_model/validations/callbacks.rb:117</summary>
192
+
193
+ ##### ActiveModel::Validations::Callbacks#run_validations!
194
+ ```ruby
195
+ def run_validations!
196
+ _run_validation_callbacks { super }
197
+ end
198
+ # called from activemodel-5.2.3/lib/active_model/validations.rb:339
199
+ ```
200
+
201
+ </details>
202
+ <details open>
203
+ <summary>activemodel-5.2.3/lib/active_model/validations.rb:408</summary>
204
+
205
+ ##### ActiveModel::Validations#run_validations!
206
+ ```ruby
207
+ def run_validations!
208
+ _run_validate_callbacks
209
+ errors.empty?
210
+ end
211
+ # called from activemodel-5.2.3/lib/active_model/validations/callbacks.rb:118
212
+ ```
213
+
214
+ </details>
215
+ <details open>
216
+ <summary>activemodel-5.2.3/lib/active_model/validations.rb:303</summary>
217
+
218
+ ##### ActiveModel::Validations#errors
219
+ ```ruby
220
+ def errors
221
+ @errors ||= Errors.new(self)
222
+ end
223
+ # called from activemodel-5.2.3/lib/active_model/validations.rb:410
224
+ ```
225
+
226
+ </details>
227
+ <details open>
228
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:209</summary>
229
+
230
+ ##### ActiveModel::Errors#empty?
231
+ ```ruby
232
+ def empty?
233
+ size.zero?
234
+ end
235
+ # called from activemodel-5.2.3/lib/active_model/validations.rb:410
236
+ ```
237
+
238
+ </details>
239
+ <details open>
240
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:179</summary>
241
+
242
+ ##### ActiveModel::Errors#size
243
+ ```ruby
244
+ def size
245
+ values.flatten.size
246
+ end
247
+ # called from activemodel-5.2.3/lib/active_model/errors.rb:210
248
+ ```
249
+
250
+ </details>
251
+ <details open>
252
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:188</summary>
253
+
254
+ ##### ActiveModel::Errors#values
255
+ ```ruby
256
+ def values
257
+ messages.select do |key, value|
258
+ !value.empty?
259
+ end.values
260
+ end
261
+ # called from activemodel-5.2.3/lib/active_model/errors.rb:180
262
+ ```
263
+
264
+ </details>
265
+ <details open>
266
+ <summary>activemodel-5.2.3/lib/active_model/validations.rb:303</summary>
267
+
268
+ ##### ActiveModel::Validations#errors
269
+ ```ruby
270
+ def errors
271
+ @errors ||= Errors.new(self)
272
+ end
273
+ # called from activerecord-5.2.3/lib/active_record/validations.rb:68
274
+ ```
275
+
276
+ </details>
277
+ <details open>
278
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:209</summary>
279
+
280
+ ##### ActiveModel::Errors#empty?
281
+ ```ruby
282
+ def empty?
283
+ size.zero?
284
+ end
285
+ # called from activerecord-5.2.3/lib/active_record/validations.rb:68
286
+ ```
287
+
288
+ </details>
289
+ <details open>
290
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:179</summary>
291
+
292
+ ##### ActiveModel::Errors#size
293
+ ```ruby
294
+ def size
295
+ values.flatten.size
296
+ end
297
+ # called from activemodel-5.2.3/lib/active_model/errors.rb:210
298
+ ```
299
+
300
+ </details>
301
+ <details open>
302
+ <summary>activemodel-5.2.3/lib/active_model/errors.rb:188</summary>
303
+
304
+ ##### ActiveModel::Errors#values
305
+ ```ruby
306
+ def values
307
+ messages.select do |key, value|
308
+ !value.empty?
309
+ end.values
310
+ end
311
+ # called from activemodel-5.2.3/lib/active_model/errors.rb:180
312
+ ```
313
+
314
+ </details>
315
+
316
+
317
+
318
+ ### Example 02. Track the lifecycle of Rails application
26
319
 
27
320
 
28
321
  ```
29
322
  % bin/rails c
30
323
 
31
324
  irb(main):001:0> env = Rack::MockRequest.env_for('http://localhost:3000/books')
32
- irb(main):002:0> TraceLocation.trace { status, headers, body = Rails.application.call(env) }
325
+ irb(main):002:0> TraceLocation.trace(format: :log) { status, headers, body = Rails.application.call(env) }
33
326
  Created at /path/to/sampleapp/log/trace_location-2019050602051557077971.log
34
327
  => true
35
328
  ```
@@ -77,91 +370,6 @@ C railties-5.2.3/lib/rails/engine.rb:522 [Rails::Engine#call]
77
370
  ..................
78
371
  ```
79
372
 
80
- ### Example 02. Track the validation process of Active Record
81
-
82
- ```
83
- % bin/rails c
84
-
85
- irb(main):001:0> book = Book.new(title: "My Book Title")
86
- irb(main):002:0> TraceLocation.trace { book.validate }
87
- Created at /path/to/sampleapp/log/trace_location-2019050104049576006131.log
88
- => true
89
- ```
90
-
91
- Then you can get a log like this:
92
-
93
- ```
94
- Logged by TraceLocation gem at 2019-05-06 02:44:29 +0900
95
- https://github.com/yhirano55/trace_location
96
-
97
- [Tracing events] C: Call, R: Return
98
-
99
- C activerecord-5.2.3/lib/active_record/validations.rb:65 [ActiveRecord::Validations#valid?]
100
- C activerecord-5.2.3/lib/active_record/validations.rb:75 [ActiveRecord::Validations#default_validation_context]
101
- C activerecord-5.2.3/lib/active_record/persistence.rb:231 [ActiveRecord::Persistence#new_record?]
102
- C activerecord-5.2.3/lib/active_record/transactions.rb:490 [ActiveRecord::Transactions#sync_with_transaction_state]
103
- C activerecord-5.2.3/lib/active_record/transactions.rb:494 [ActiveRecord::Transactions#update_attributes_from_transaction_state]
104
- R activerecord-5.2.3/lib/active_record/transactions.rb:500 [ActiveRecord::Transactions#update_attributes_from_transaction_state]
105
- R activerecord-5.2.3/lib/active_record/transactions.rb:492 [ActiveRecord::Transactions#sync_with_transaction_state]
106
- R activerecord-5.2.3/lib/active_record/persistence.rb:234 [ActiveRecord::Persistence#new_record?]
107
- R activerecord-5.2.3/lib/active_record/validations.rb:77 [ActiveRecord::Validations#default_validation_context]
108
- C activemodel-5.2.3/lib/active_model/validations.rb:336 [ActiveModel::Validations#valid?]
109
- C activemodel-5.2.3/lib/active_model/validations.rb:303 [ActiveModel::Validations#errors]
110
- C activemodel-5.2.3/lib/active_model/errors.rb:74 [ActiveModel::Errors#initialize]
111
- C activemodel-5.2.3/lib/active_model/errors.rb:470 [ActiveModel::Errors#apply_default_array]
112
- R activemodel-5.2.3/lib/active_model/errors.rb:473 [ActiveModel::Errors#apply_default_array]
113
- C activemodel-5.2.3/lib/active_model/errors.rb:470 [ActiveModel::Errors#apply_default_array]
114
- R activemodel-5.2.3/lib/active_model/errors.rb:473 [ActiveModel::Errors#apply_default_array]
115
- R activemodel-5.2.3/lib/active_model/errors.rb:78 [ActiveModel::Errors#initialize]
116
- R activemodel-5.2.3/lib/active_model/validations.rb:305 [ActiveModel::Validations#errors]
117
- C activemodel-5.2.3/lib/active_model/errors.rb:115 [ActiveModel::Errors#clear]
118
- R activemodel-5.2.3/lib/active_model/errors.rb:118 [ActiveModel::Errors#clear]
119
- C activemodel-5.2.3/lib/active_model/validations/callbacks.rb:117 [ActiveModel::Validations::Callbacks#run_validations!]
120
- C activesupport-5.2.3/lib/active_support/callbacks.rb:815 [ActiveRecord::Base#_run_validation_callbacks]
121
- C activesupport-5.2.3/lib/active_support/callbacks.rb:94 [ActiveSupport::Callbacks#run_callbacks]
122
- C activesupport-5.2.3/lib/active_support/core_ext/class/attribute.rb:124 [ActiveRecord::Base#__callbacks]
123
- C activesupport-5.2.3/lib/active_support/core_ext/class/attribute.rb:106 [ActiveRecord::Base.__callbacks]
124
- R activesupport-5.2.3/lib/active_support/core_ext/class/attribute.rb:128 [ActiveRecord::Base.__callbacks]
125
- R activesupport-5.2.3/lib/active_support/callbacks.rb:95 [ActiveRecord::Base#__callbacks]
126
- C activesupport-5.2.3/lib/active_support/callbacks.rb:539 [ActiveSupport::Callbacks::CallbackChain#empty?]
127
- R activesupport-5.2.3/lib/active_support/callbacks.rb:539 [ActiveSupport::Callbacks::CallbackChain#empty?]
128
- C activemodel-5.2.3/lib/active_model/validations.rb:408 [ActiveModel::Validations#run_validations!]
129
- C activesupport-5.2.3/lib/active_support/callbacks.rb:815 [ActiveRecord::Base#_run_validate_callbacks]
130
- C activesupport-5.2.3/lib/active_support/callbacks.rb:94 [ActiveSupport::Callbacks#run_callbacks]
131
- C activesupport-5.2.3/lib/active_support/core_ext/class/attribute.rb:124 [ActiveRecord::Base#__callbacks]
132
- C activesupport-5.2.3/lib/active_support/core_ext/class/attribute.rb:106 [ActiveRecord::Base.__callbacks]
133
- R activesupport-5.2.3/lib/active_support/core_ext/class/attribute.rb:128 [ActiveRecord::Base.__callbacks]
134
- R activesupport-5.2.3/lib/active_support/callbacks.rb:95 [ActiveRecord::Base#__callbacks]
135
- C activesupport-5.2.3/lib/active_support/callbacks.rb:539 [ActiveSupport::Callbacks::CallbackChain#empty?]
136
- R activesupport-5.2.3/lib/active_support/callbacks.rb:539 [ActiveSupport::Callbacks::CallbackChain#empty?]
137
- R activesupport-5.2.3/lib/active_support/callbacks.rb:139 [ActiveSupport::Callbacks#run_callbacks]
138
- R activesupport-5.2.3/lib/active_support/callbacks.rb:817 [ActiveRecord::Base#_run_validate_callbacks]
139
- C activemodel-5.2.3/lib/active_model/validations.rb:303 [ActiveModel::Validations#errors]
140
- R activemodel-5.2.3/lib/active_model/validations.rb:305 [ActiveModel::Validations#errors]
141
- C activemodel-5.2.3/lib/active_model/errors.rb:209 [ActiveModel::Errors#empty?]
142
- C activemodel-5.2.3/lib/active_model/errors.rb:179 [ActiveModel::Errors#size]
143
- C activemodel-5.2.3/lib/active_model/errors.rb:188 [ActiveModel::Errors#values]
144
- R activemodel-5.2.3/lib/active_model/errors.rb:192 [ActiveModel::Errors#values]
145
- R activemodel-5.2.3/lib/active_model/errors.rb:181 [ActiveModel::Errors#size]
146
- R activemodel-5.2.3/lib/active_model/errors.rb:211 [ActiveModel::Errors#empty?]
147
- R activemodel-5.2.3/lib/active_model/validations.rb:411 [ActiveModel::Validations#run_validations!]
148
- R activesupport-5.2.3/lib/active_support/callbacks.rb:139 [ActiveSupport::Callbacks#run_callbacks]
149
- R activesupport-5.2.3/lib/active_support/callbacks.rb:817 [ActiveRecord::Base#_run_validation_callbacks]
150
- R activemodel-5.2.3/lib/active_model/validations/callbacks.rb:119 [ActiveModel::Validations::Callbacks#run_validations!]
151
- R activemodel-5.2.3/lib/active_model/validations.rb:342 [ActiveModel::Validations#valid?]
152
- C activemodel-5.2.3/lib/active_model/validations.rb:303 [ActiveModel::Validations#errors]
153
- R activemodel-5.2.3/lib/active_model/validations.rb:305 [ActiveModel::Validations#errors]
154
- C activemodel-5.2.3/lib/active_model/errors.rb:209 [ActiveModel::Errors#empty?]
155
- C activemodel-5.2.3/lib/active_model/errors.rb:179 [ActiveModel::Errors#size]
156
- C activemodel-5.2.3/lib/active_model/errors.rb:188 [ActiveModel::Errors#values]
157
- R activemodel-5.2.3/lib/active_model/errors.rb:192 [ActiveModel::Errors#values]
158
- R activemodel-5.2.3/lib/active_model/errors.rb:181 [ActiveModel::Errors#size]
159
- R activemodel-5.2.3/lib/active_model/errors.rb:211 [ActiveModel::Errors#empty?]
160
- R activerecord-5.2.3/lib/active_record/validations.rb:69 [ActiveRecord::Validations#valid?]
161
-
162
- Result: true
163
- ```
164
-
165
373
  ## License
166
374
 
167
375
  [MIT License](https://opensource.org/licenses/MIT)
@@ -1,22 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'binding_of_caller'
4
+
3
5
  module TraceLocation
4
6
  module Collector # :nodoc:
5
7
  require_relative 'event'
6
8
  Result = Struct.new(:events, :return_value)
7
9
 
8
- def self.collect(&block)
10
+ def self.collect(match:, ignore:, &block)
9
11
  events = []
10
12
  hierarchy = 0
13
+ id = 0
14
+ cache = {}
15
+
11
16
  tracer = TracePoint.new(:call, :return) do |trace_point|
12
- next unless trace_point.path.include?(::TraceLocation.config.gems_dir)
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}"
13
23
 
14
24
  case trace_point.event
15
25
  when :call
26
+ cache[location_cache_key] = hierarchy
27
+
16
28
  events << Event.new(
29
+ id: id,
17
30
  event: trace_point.event,
18
31
  path: trace_point.path,
19
32
  lineno: trace_point.lineno,
33
+ caller_path: caller_path,
34
+ caller_lineno: caller_lineno,
20
35
  method_id: trace_point.method_id,
21
36
  defined_class: trace_point.defined_class,
22
37
  hierarchy: hierarchy
@@ -24,12 +39,15 @@ module TraceLocation
24
39
 
25
40
  hierarchy += 1
26
41
  when :return
27
- hierarchy -= 1
42
+ hierarchy = cache[location_cache_key] || hierarchy
28
43
 
29
44
  events << Event.new(
45
+ id: id,
30
46
  event: trace_point.event,
31
47
  path: trace_point.path,
32
48
  lineno: trace_point.lineno,
49
+ caller_path: caller_path,
50
+ caller_lineno: caller_lineno,
33
51
  method_id: trace_point.method_id,
34
52
  defined_class: trace_point.defined_class,
35
53
  hierarchy: hierarchy
@@ -7,7 +7,7 @@ module TraceLocation
7
7
  def initialize
8
8
  @gems_dir = File.join(Gem.path[0], 'gems')
9
9
  @dest_dir = Dir.pwd
10
- @default_format = :log
10
+ @default_format = :markdown
11
11
  end
12
12
  end
13
13
  end
@@ -3,12 +3,15 @@
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 :event, :path, :lineno, :method_id, :defined_class, :hierarchy
6
+ attr_reader :id, :event, :path, :lineno, :caller_path, :caller_lineno, :method_id, :defined_class, :hierarchy
7
7
 
8
- def initialize(event:, path:, lineno:, method_id:, defined_class:, hierarchy:)
8
+ def initialize(id:, event:, path:, lineno:, caller_path:, caller_lineno:, method_id:, defined_class:, hierarchy:)
9
+ @id = id
9
10
  @event = event
10
11
  @path = path
11
12
  @lineno = lineno
13
+ @caller_path = caller_path
14
+ @caller_lineno = caller_lineno
12
15
  @method_id = method_id
13
16
  @defined_class = defined_class
14
17
  @hierarchy = hierarchy.to_i
@@ -5,9 +5,10 @@ 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
9
+
8
10
  def initialize(events, return_value, options)
9
- @events = events
10
- @return_value = return_value
11
+ super
11
12
  @dest_dir = options.fetch(:dest_dir) { ::TraceLocation.config.dest_dir }
12
13
  @file_path = File.join(@dest_dir, "trace_location-#{Time.now.strftime('%Y%m%d%H%m%s')}.csv")
13
14
  end
@@ -28,9 +29,10 @@ module TraceLocation
28
29
 
29
30
  def create_file
30
31
  CSV.open(file_path, 'wb+') do |csv|
31
- csv << %w[event method path lineno]
32
- events.each do |e|
33
- csv << [e.event, e.method_str, e.path, e.lineno]
32
+ csv << ATTRIBUTES
33
+
34
+ events.each do |event|
35
+ csv << ATTRIBUTES.map { |attr| event.public_send(attr) }
34
36
  end
35
37
  end
36
38
  end
@@ -10,8 +10,7 @@ module TraceLocation
10
10
  INDENT_STRING = ' '
11
11
 
12
12
  def initialize(events, return_value, options)
13
- @events = events
14
- @return_value = return_value
13
+ super
15
14
  @gems_dir = ::TraceLocation.config.gems_dir
16
15
  @dest_dir = options.fetch(:dest_dir) { ::TraceLocation.config.dest_dir }
17
16
  @current = Time.now
@@ -34,7 +33,7 @@ module TraceLocation
34
33
  end
35
34
 
36
35
  def create_file
37
- File.open(file_path, 'w+') do |io|
36
+ File.open(file_path, 'wb+') do |io|
38
37
  io.puts "Logged by TraceLocation gem at #{current}"
39
38
  io.puts 'https://github.com/yhirano55/trace_location'
40
39
  io.puts
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
4
+
5
+ module TraceLocation
6
+ module Generator
7
+ class Markdown < Base # :nodoc:
8
+ def initialize(events, return_value, options)
9
+ super
10
+ @gems_dir = ::TraceLocation.config.gems_dir
11
+ @dest_dir = options.fetch(:dest_dir) { ::TraceLocation.config.dest_dir }
12
+ @current = Time.now
13
+ @filename = "trace_location-#{@current.strftime('%Y%m%d%H%m%s')}.md"
14
+ @file_path = File.join(@dest_dir, @filename)
15
+ end
16
+
17
+ def generate
18
+ setup_dir
19
+ create_file
20
+ $stdout.puts "Created at #{file_path}"
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :events, :return_value, :gems_dir, :dest_dir, :current, :filename, :file_path
26
+
27
+ def setup_dir
28
+ FileUtils.mkdir_p(dest_dir)
29
+ end
30
+
31
+ def create_file
32
+ File.open(file_path, 'wb+') do |io|
33
+ io.write <<~MARKDOWN
34
+ Generated by [trace_location](https://github.com/yhirano55/trace_location) at #{current}
35
+
36
+ MARKDOWN
37
+
38
+ events.select(&:call?).each do |e|
39
+ pm = begin
40
+ Pry::Method.from_str(e.method_str)
41
+ rescue StandardError
42
+ nil
43
+ end
44
+ next if pm.nil?
45
+
46
+ path = e.path.to_s.gsub(%r{#{gems_dir}/}, '')
47
+ caller_path = e.caller_path.to_s.gsub(%r{#{gems_dir}/}, '')
48
+ io.write <<~MARKDOWN
49
+ <details open>
50
+ <summary>#{path}:#{e.lineno}</summary>
51
+
52
+ ##### #{pm.name_with_owner}
53
+ ```#{pm.source_type}
54
+ #{pm.source.chomp}
55
+ # called from #{caller_path}:#{e.caller_lineno}
56
+ ```
57
+
58
+ </details>
59
+ MARKDOWN
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -5,5 +5,6 @@ module TraceLocation
5
5
  require_relative 'generator/base'
6
6
  require_relative 'generator/csv'
7
7
  require_relative 'generator/log'
8
+ require_relative 'generator/markdown'
8
9
  end
9
10
  end
@@ -8,7 +8,8 @@ module TraceLocation
8
8
 
9
9
  GENERATORS = {
10
10
  csv: ::TraceLocation::Generator::Csv,
11
- log: ::TraceLocation::Generator::Log
11
+ log: ::TraceLocation::Generator::Log,
12
+ markdown: ::TraceLocation::Generator::Markdown
12
13
  }.freeze
13
14
 
14
15
  def self.build(events, return_value, options)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TraceLocation
4
- VERSION = '0.4.0'
4
+ VERSION = '0.9.0'
5
5
  end
@@ -14,7 +14,10 @@ end
14
14
 
15
15
  module TraceLocation # :nodoc:
16
16
  def self.trace(options = {}, &block)
17
- result = Collector.collect(&block)
17
+ match = options.delete(:match)
18
+ ignore = options.delete(:ignore)
19
+
20
+ result = Collector.collect(match: match, ignore: ignore, &block)
18
21
  Report.build(result.events, result.return_value, options).generate
19
22
  true
20
23
  rescue StandardError => e
@@ -23,7 +23,10 @@ Gem::Specification.new do |s|
23
23
  s.require_paths = ['lib']
24
24
  s.required_ruby_version = '>= 2.5.0'
25
25
 
26
- s.add_development_dependency 'bundler', '~> 2.0'
27
- s.add_development_dependency 'rake', '~> 10.0'
28
- s.add_development_dependency 'rspec', '~> 3.0'
26
+ s.add_dependency 'binding_of_caller'
27
+ s.add_dependency 'pry'
28
+
29
+ s.add_development_dependency 'bundler'
30
+ s.add_development_dependency 'rake'
31
+ s.add_development_dependency 'rspec'
29
32
  end
metadata CHANGED
@@ -1,57 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trace_location
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.9.0
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-05-28 00:00:00.000000000 Z
11
+ date: 2019-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: binding_of_caller
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
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'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
16
44
  requirements:
17
- - - "~>"
45
+ - - ">="
18
46
  - !ruby/object:Gem::Version
19
- version: '2.0'
47
+ version: '0'
20
48
  type: :development
21
49
  prerelease: false
22
50
  version_requirements: !ruby/object:Gem::Requirement
23
51
  requirements:
24
- - - "~>"
52
+ - - ">="
25
53
  - !ruby/object:Gem::Version
26
- version: '2.0'
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
30
58
  requirements:
31
- - - "~>"
59
+ - - ">="
32
60
  - !ruby/object:Gem::Version
33
- version: '10.0'
61
+ version: '0'
34
62
  type: :development
35
63
  prerelease: false
36
64
  version_requirements: !ruby/object:Gem::Requirement
37
65
  requirements:
38
- - - "~>"
66
+ - - ">="
39
67
  - !ruby/object:Gem::Version
40
- version: '10.0'
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: rspec
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
- - - "~>"
73
+ - - ">="
46
74
  - !ruby/object:Gem::Version
47
- version: '3.0'
75
+ version: '0'
48
76
  type: :development
49
77
  prerelease: false
50
78
  version_requirements: !ruby/object:Gem::Requirement
51
79
  requirements:
52
- - - "~>"
80
+ - - ">="
53
81
  - !ruby/object:Gem::Version
54
- version: '3.0'
82
+ version: '0'
55
83
  description: TraceLocation helps you get tracing the source location of codes, and
56
84
  helps you can get reading the huge open souce libraries in Ruby
57
85
  email:
@@ -80,6 +108,7 @@ files:
80
108
  - lib/trace_location/generator/base.rb
81
109
  - lib/trace_location/generator/csv.rb
82
110
  - lib/trace_location/generator/log.rb
111
+ - lib/trace_location/generator/markdown.rb
83
112
  - lib/trace_location/railtie.rb
84
113
  - lib/trace_location/report.rb
85
114
  - lib/trace_location/version.rb