tapping_device 0.4.5 → 0.4.6

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: 89e8a71fd5423c54c59b6b46f672e228aa72e413173a79885c92ea5c5839c8b6
4
- data.tar.gz: 83960e79383e8f892e060f892be301a54744c177f36219d3a24828e734d0ffb3
3
+ metadata.gz: ee22ec85fa4fc7f9b96a1e13a067a581e4dc6dcd591783019c4bae8e829cda10
4
+ data.tar.gz: ec3805395e9dc95b31cdb30d9c867877d37aada6c5b12d2ac842e3fdbc9e563d
5
5
  SHA512:
6
- metadata.gz: b3917a20a5f63ba529bf5834b09d5446ecde966fb033e682460fcad47be6861912bfc4d908c2d16b0e5d567a179a469a7e173adb196acdf6a217a44fee05a579
7
- data.tar.gz: 53998f52cbfbe390e047237236dc26646803b0a0d59428e4d73d85319fb820a27e64832082ee0a328f87f514f7de6cbc4f9d08737dfe44ee644b798a87fb6d10
6
+ metadata.gz: 59c1d8868d94bbea441a64291a77ec1783f146d584234657c8b455073b17a760cda0a035789c8d73d1b6cc9bbfb7a28f1afb619dc026ec1ac970917ca73514a1
7
+ data.tar.gz: 75b0ba8536f9fdf5f8bce8c990d5094881a2206763ffb8ab19ef9fe549944442a4f42b9dd26d3a2c4564cf1a43dafcb7ff4531c5281fc1488b8d0d725612693c
data/Gemfile.lock CHANGED
@@ -1,18 +1,18 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tapping_device (0.4.5)
4
+ tapping_device (0.4.6)
5
5
  activerecord (>= 5.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (6.0.2)
11
- activesupport (= 6.0.2)
12
- activerecord (6.0.2)
13
- activemodel (= 6.0.2)
14
- activesupport (= 6.0.2)
15
- activesupport (6.0.2)
10
+ activemodel (6.0.2.1)
11
+ activesupport (= 6.0.2.1)
12
+ activerecord (6.0.2.1)
13
+ activemodel (= 6.0.2.1)
14
+ activesupport (= 6.0.2.1)
15
+ activesupport (6.0.2.1)
16
16
  concurrent-ruby (~> 1.0, >= 1.0.2)
17
17
  i18n (>= 0.7, < 2)
18
18
  minitest (~> 5.1)
@@ -45,7 +45,7 @@ GEM
45
45
  rspec-support (3.8.2)
46
46
  sqlite3 (1.4.1)
47
47
  thread_safe (0.3.6)
48
- tzinfo (1.2.5)
48
+ tzinfo (1.2.6)
49
49
  thread_safe (~> 0.1)
50
50
  zeitwerk (2.2.2)
51
51
 
data/README.md CHANGED
@@ -2,10 +2,27 @@
2
2
 
3
3
  ![](https://github.com/st0012/tapping_device/workflows/Ruby/badge.svg)
4
4
 
5
+ ## Related Posts
6
+ - [Debug Rails issues effectively with tapping_device](https://dev.to/st0012/debug-rails-issues-effectively-with-tappingdevice-c7c)
7
+ - [Want to know more about your Rails app? Tap on your objects!](https://dev.to/st0012/want-to-know-more-about-your-rails-app-tap-on-your-objects-bd3)
8
+
9
+ ## Table of Content
10
+ - [Introduction](#introduction)
11
+ - [Track Method Calls](#track-method-calls)
12
+ - [Track Association Calls](#track-association-calls)
13
+ - [Track Calls that Generates SQL Queries](#track-calls-that-generates-sql-queries)
14
+ - [Installation](#installation)
15
+ - [Usages](#usage)
16
+ - [Methods](#methods)
17
+ - [Payload](#payload-of-the-call)
18
+ - [Options](#options)
19
+ - [Advance Usages](#advance-usages)
20
+
21
+ ## Introduction
5
22
 
6
23
  `tapping_device` is a gem built on top of Ruby’s `TracePoint` class that allows you to tap method calls of specified objects. The purpose for this gem is to make debugging Rails applications easier. Here are some sample usages:
7
24
 
8
- ### Track method calls
25
+ ### Track Method Calls
9
26
 
10
27
  ```ruby
11
28
  class PostsController < ApplicationController
@@ -13,10 +30,7 @@ class PostsController < ApplicationController
13
30
 
14
31
  def show
15
32
  @post = Post.find(params[:id])
16
- tap_on!(@post) do |payload|
17
- # this equals to `"Method: :#{payload[:method_name]}, line: #{payload[:filepath]}:#{payload[:line_number]}"`
18
- puts(payload.method_name_and_location)
19
- end
33
+ tap_on!(@post).and_print(:method_name_and_location)
20
34
  end
21
35
  end
22
36
  ```
@@ -24,32 +38,30 @@ end
24
38
  And you can see these in log:
25
39
 
26
40
  ```
27
- Method: :name, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
28
- Method: :user_id, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
29
- Method: :to_param, line: /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
41
+ name FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
42
+ user_id FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
43
+ to_param FROM /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
30
44
  ```
31
45
 
32
46
 
33
- ### Track ActiveRecord records’ association calls
47
+ ### Track Association Calls
34
48
 
35
49
  Or you can use `tap_assoc!`. This is very useful for tracking potential n+1 query calls, here’s a sample from my work
36
50
 
37
51
  ```ruby
38
- tap_assoc!(order) do |payload|
39
- puts(payload.method_name_and_location)
40
- end
52
+ tap_assoc!(order).and_print(:method_name_and_location)
41
53
  ```
42
54
 
43
55
  ```
44
- Method: :payments, line: /RUBY_PATH/gems/2.6.0/gems/jsonapi-resources-0.9.10/lib/jsonapi/resource.rb:124
45
- Method: :line_items, line: /MY_PROJECT/app/models/line_item_container_helpers.rb:44
46
- Method: :effective_line_items, line: /MY_PROJECT/app/models/line_item_container_helpers.rb:110
47
- Method: :amending_orders, line: /MY_PROJECT/app/models/order.rb:385
48
- Method: :amends_order, line: /MY_PROJECT/app/models/order.rb:432
56
+ payments FROM /RUBY_PATH/gems/2.6.0/gems/jsonapi-resources-0.9.10/lib/jsonapi/resource.rb:124
57
+ line_items FROM /MY_PROJECT/app/models/line_item_container_helpers.rb:44
58
+ effective_line_items FROM /MY_PROJECT/app/models/line_item_container_helpers.rb:110
59
+ amending_orders FROM /MY_PROJECT/app/models/order.rb:385
60
+ amends_order FROM /MY_PROJECT/app/models/order.rb:432
49
61
  ```
50
62
 
51
63
 
52
- ### Track calls that generates sql queries!
64
+ ### Track Calls that Generates SQL Queries
53
65
 
54
66
  `tap_sql!` method helps you track which method calls generate sql queries. This is particularly helpful when tracking calls created from a reused `ActiveRecord::Relation` object.
55
67
 
@@ -135,9 +147,18 @@ All tapping methods (start with `tap_`) takes a block and yield a `Payload` obje
135
147
  - `trace` - stack trace of the call. Default is an empty array unless `with_trace_to` option is set
136
148
  - `tp` - trace point object of this call
137
149
 
138
- #### Some useful helpers
139
- - `method_name_and_location` - `"Method: :initialize, line: /PROJECT_PATH/tapping_device/spec/payload_spec.rb:7"`
140
- - `method_name_and_arguments` - `"Method: :initialize, argments: {:name=>\"Stan\", :age=>25}"`
150
+
151
+ #### Symbols for helpers
152
+ - `FROM` for method call’s location
153
+ - `<=` for arguments
154
+ - `=>` for return value
155
+ - `@` for defined class
156
+
157
+ #### Helpers
158
+ - `method_name_and_location` - `initialize FROM /PROJECT_PATH/tapping_device/spec/payload_spec.rb:7`
159
+ - `method_name_and_arguments` - `initialize <= {:name=>\"Stan\", :age=>25}`
160
+ - `method_name_and_return_value` - `ten => 10`
161
+ - `method_name_and_defined_class` - `initialize @ Student`
141
162
  - `passed_at` -
142
163
  ```
143
164
  Passed as 'object' in method ':initialize'
@@ -152,6 +173,15 @@ Passed as 'object' in method ':initialize'
152
173
  at /Users/st0012/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/actionview-6.0.0/lib/action_view/helpers/tags/label.rb:60
153
174
  ```
154
175
 
176
+ - `detail_call_info`
177
+
178
+ ```
179
+ initialize @ Student
180
+ <= {:name=>"Stan", :age=>25}
181
+ => 25
182
+ FROM /Users/st0012/projects/tapping_device/spec/payload_spec.rb:7
183
+ ```
184
+
155
185
 
156
186
  ### Options
157
187
  - `with_trace_to: 10` - the number of traces we want to put into `trace`. Default is `nil`, so `trace` would be empty
@@ -159,24 +189,21 @@ Passed as 'object' in method ':initialize'
159
189
  - `filter_by_paths: [/path/]` - only contain calls from the specified paths
160
190
 
161
191
  ```ruby
162
- tap_on!(@post, exclude_by_paths: [/active_record/]) do |payload|
163
- # this equals to `"Method: #{payload[:method_name]} line: #{payload[:filepath]}:#{payload[:line_number]}"`
164
- puts(payload.method_name_and_location)
165
- end
192
+ tap_on!(@post, exclude_by_paths: [/active_record/]).and_print(:method_name_and_location)
166
193
  ```
167
194
 
168
195
  ```
169
- Method: :_read_attribute, line: /RUBY_PATH/gems/2.6.0/gems/activerecord-5.2.0/lib/active_record/attribute_methods/read.rb:40
170
- Method: :name, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
171
- Method: :_read_attribute, line: /RUBY_PATH/gems/2.6.0/gems/activerecord-5.2.0/lib/active_record/attribute_methods/read.rb:40
172
- Method: :user_id, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
196
+ _read_attribute FROM /RUBY_PATH/gems/2.6.0/gems/activerecord-5.2.0/lib/active_record/attribute_methods/read.rb:40
197
+ name FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
198
+ _read_attribute FROM /RUBY_PATH/gems/2.6.0/gems/activerecord-5.2.0/lib/active_record/attribute_methods/read.rb:40
199
+ user_id FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
173
200
  .......
174
201
 
175
202
  # versus
176
203
 
177
- Method: :name, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
178
- Method: :user_id, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
179
- Method: :to_param, line: /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
204
+ name FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
205
+ user_id FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
206
+ to_param FROM /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
180
207
  ```
181
208
 
182
209
 
@@ -203,10 +230,7 @@ class PostsController < ApplicationController
203
230
  before_action :set_post, only: [:show, :edit, :update, :destroy]
204
231
 
205
232
  def show
206
- tap_on!(@post) do |payload|
207
- # this equals to `"Method: #{payload[:method_name]} line: #{payload[:filepath]}:#{payload[:line_number]}"`
208
- puts(payload.method_name_and_location)
209
- end
233
+ tap_on!(@post).and_print(:method_name_and_location)
210
234
  end
211
235
  end
212
236
  ```
@@ -214,9 +238,9 @@ end
214
238
  And you can see these in log:
215
239
 
216
240
  ```
217
- Method: :name, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
218
- Method: :user_id, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
219
- Method: :to_param, line: /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
241
+ name FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
242
+ user_id FROM /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
243
+ to_param FROM /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
220
244
  ```
221
245
 
222
246
  ### `tap_passed!`
@@ -255,17 +279,15 @@ Passed as 'record_or_hash_or_array' in method ':polymorphic_method'
255
279
  ### `tap_assoc!`
256
280
 
257
281
  ```ruby
258
- tap_assoc!(order) do |payload|
259
- puts(payload.method_name_and_location)
260
- end
282
+ tap_assoc!(order).and_print(:method_name_and_location)
261
283
  ```
262
284
 
263
285
  ```
264
- Method: :payments, line: /RUBY_PATH/gems/2.6.0/gems/jsonapi-resources-0.9.10/lib/jsonapi/resource.rb:124
265
- Method: :line_items, line: /MY_PROJECT/app/models/line_item_container_helpers.rb:44
266
- Method: :effective_line_items, line: /MY_PROJECT/app/models/line_item_container_helpers.rb:110
267
- Method: :amending_orders, line: /MY_PROJECT/app/models/order.rb:385
268
- Method: :amends_order, line: /MY_PROJECT/app/models/order.rb:432
286
+ payments FROM /RUBY_PATH/gems/2.6.0/gems/jsonapi-resources-0.9.10/lib/jsonapi/resource.rb:124
287
+ line_items FROM /MY_PROJECT/app/models/line_item_container_helpers.rb:44
288
+ effective_line_items FROM /MY_PROJECT/app/models/line_item_container_helpers.rb:110
289
+ amending_orders FROM /MY_PROJECT/app/models/order.rb:385
290
+ amends_order FROM /MY_PROJECT/app/models/order.rb:432
269
291
  ```
270
292
 
271
293
  ### `tap_sql!`
@@ -37,12 +37,26 @@ class TappingDevice
37
37
  "#{filepath}:#{line_number}"
38
38
  end
39
39
 
40
- def method_name_and_location
41
- "Method: :#{method_name}, line: #{location}"
40
+ SYMBOLS = {
41
+ location: "FROM",
42
+ return_value: "=>",
43
+ arguments: "<=",
44
+ defined_class: "@"
45
+ }
46
+
47
+ SYMBOLS.each do |name, symbol|
48
+ define_method "method_name_and_#{name}" do
49
+ "#{method_name} #{symbol} #{send(name)}"
50
+ end
42
51
  end
43
52
 
44
- def method_name_and_arguments
45
- "Method: :#{method_name}, argments: #{arguments.to_s}"
53
+ def detail_call_info
54
+ <<~MSG
55
+ #{method_name_and_defined_class}
56
+ <= #{arguments}
57
+ => #{return_value || "nil"}
58
+ FROM #{location}
59
+ MSG
46
60
  end
47
61
  end
48
62
  end
@@ -1,3 +1,3 @@
1
1
  class TappingDevice
2
- VERSION = "0.4.5"
2
+ VERSION = "0.4.6"
3
3
  end
@@ -21,6 +21,7 @@ class TappingDevice
21
21
 
22
22
  def initialize(options = {}, &block)
23
23
  @block = block
24
+ @output_block = nil
24
25
  @options = process_options(options)
25
26
  @calls = []
26
27
  @disabled = false
@@ -45,6 +46,10 @@ class TappingDevice
45
46
  track(record, condition: :tap_associations?)
46
47
  end
47
48
 
49
+ def and_print(payload_method)
50
+ @output_block = -> (payload) { puts(payload.send(payload_method)) }
51
+ end
52
+
48
53
  def set_block(&block)
49
54
  @block = block
50
55
  end
@@ -208,6 +213,8 @@ class TappingDevice
208
213
  def record_call!(payload)
209
214
  return if @disabled
210
215
 
216
+ @output_block.call(payload) if @output_block
217
+
211
218
  if @block
212
219
  root_device.calls << @block.call(payload)
213
220
  else
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tapping_device
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - st0012
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-15 00:00:00.000000000 Z
11
+ date: 2019-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord