tapping_device 0.4.0 → 0.4.1
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 +4 -4
- data/.github/workflows/ruby.yml +1 -0
- data/Gemfile.lock +2 -2
- data/README.md +37 -30
- data/lib/tapping_device/payload.rb +27 -0
- data/lib/tapping_device/version.rb +1 -1
- data/lib/tapping_device.rb +3 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65b116ba192b195aca0df0f0400e8d2b0617b221c620dcaaf8eab0f16e2d85fb
|
4
|
+
data.tar.gz: 0cb2b12d7329f5f770bd8d1522b8ab0d5f7c1787d142d06116cf83908e136f63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7192f5957a150267f71b4fe88a9eabb45f0966d5fda933e2a050f8a52e823ae338e6a3e35f24ff6f3ccef9f3b96c8fca9df300b8e3b3c4e69d29964f5ebdaa91
|
7
|
+
data.tar.gz: 264dd2bd5c83c1f716d272179c296a03bdf7612d2415602174432817769fa8f069612af45c0f58da722a0e499fe06bc440d3f1810bcc1a810e8c238e27fd97c5
|
data/.github/workflows/ruby.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
tapping_device (0.
|
4
|
+
tapping_device (0.4.1)
|
5
5
|
activerecord (>= 5.2)
|
6
6
|
|
7
7
|
GEM
|
@@ -47,7 +47,7 @@ GEM
|
|
47
47
|
thread_safe (0.3.6)
|
48
48
|
tzinfo (1.2.5)
|
49
49
|
thread_safe (~> 0.1)
|
50
|
-
zeitwerk (2.2.
|
50
|
+
zeitwerk (2.2.2)
|
51
51
|
|
52
52
|
PLATFORMS
|
53
53
|
ruby
|
data/README.md
CHANGED
@@ -15,7 +15,8 @@ class PostsController < ApplicationController
|
|
15
15
|
def show
|
16
16
|
@post = Post.find(params[:id])
|
17
17
|
tap_on!(@post) do |payload|
|
18
|
-
|
18
|
+
# this equals to `"Method: :#{payload[:method_name]}, line: #{payload[:filepath]}:#{payload[:line_number]}"`
|
19
|
+
puts(payload.method_name_and_location)
|
19
20
|
end
|
20
21
|
end
|
21
22
|
end
|
@@ -24,9 +25,9 @@ end
|
|
24
25
|
And you can see these in log:
|
25
26
|
|
26
27
|
```
|
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
|
28
|
+
Method: :name, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
|
29
|
+
Method: :user_id, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
|
30
|
+
Method: :to_param, line: /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
|
30
31
|
```
|
31
32
|
|
32
33
|
|
@@ -36,16 +37,16 @@ Or you can use `tap_assoc!`. This is very useful for tracking potential n+1 quer
|
|
36
37
|
|
37
38
|
```ruby
|
38
39
|
tap_assoc!(order) do |payload|
|
39
|
-
puts
|
40
|
+
puts(payload.method_name_and_location)
|
40
41
|
end
|
41
42
|
```
|
42
43
|
|
43
44
|
```
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
Method: :payments, line: /RUBY_PATH/gems/2.6.0/gems/jsonapi-resources-0.9.10/lib/jsonapi/resource.rb:124
|
46
|
+
Method: :line_items, line: /MY_PROJECT/app/models/line_item_container_helpers.rb:44
|
47
|
+
Method: :effective_line_items, line: /MY_PROJECT/app/models/line_item_container_helpers.rb:110
|
48
|
+
Method: :amending_orders, line: /MY_PROJECT/app/models/order.rb:385
|
49
|
+
Method: :amends_order, line: /MY_PROJECT/app/models/order.rb:432
|
49
50
|
```
|
50
51
|
|
51
52
|
|
@@ -118,8 +119,8 @@ In order to use `tapping_device`, you need to include `TappingDevice::Trackable`
|
|
118
119
|
- `tap_assoc!(activerecord_object)` - tracks association calls on a record, like `post.comments`
|
119
120
|
- `tap_sql!(activerecord_relation_or_model)` - tracks sql queries generated from the target
|
120
121
|
|
121
|
-
###
|
122
|
-
All tapping methods (start with `tap_`) takes a block and yield a
|
122
|
+
### Payload of the call
|
123
|
+
All tapping methods (start with `tap_`) takes a block and yield a `Payload` object as block argument. The `Payload` class inherits `Hash` so we can either use it as a hash, or you can call its keys as methods.
|
123
124
|
|
124
125
|
```ruby
|
125
126
|
{
|
@@ -149,6 +150,10 @@ The hash contains
|
|
149
150
|
- `trace` - stack trace of the call. Default is an empty array unless `with_trace_to` option is set
|
150
151
|
- `tp` - trace point object of this call
|
151
152
|
|
153
|
+
#### Some useful helpers
|
154
|
+
- `method_name_and_location` - `"Method: :initialize, line: /PROJECT_PATH/tapping_device/spec/payload_spec.rb:7"`
|
155
|
+
- `method_name_and_arguments` - `"Method: :initialize, argments: [[:name, \"Stan\"], [:age, 25]]"`
|
156
|
+
|
152
157
|
|
153
158
|
### Options
|
154
159
|
- `with_trace_to: 10` - the number of traces we want to put into `trace`. Default is `nil`, so `trace` would be empty
|
@@ -157,22 +162,23 @@ The hash contains
|
|
157
162
|
|
158
163
|
```ruby
|
159
164
|
tap_on!(@post, exclude_by_paths: [/active_record/]) do |payload|
|
160
|
-
|
165
|
+
# this equals to `"Method: #{payload[:method_name]} line: #{payload[:filepath]}:#{payload[:line_number]}"`
|
166
|
+
puts(payload.method_name_and_location)
|
161
167
|
end
|
162
168
|
```
|
163
169
|
|
164
170
|
```
|
165
|
-
Method: _read_attribute line: /RUBY_PATH/gems/2.6.0/gems/activerecord-5.2.0/lib/active_record/attribute_methods/read.rb:40
|
166
|
-
Method: name line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
|
167
|
-
Method: _read_attribute line: /RUBY_PATH/gems/2.6.0/gems/activerecord-5.2.0/lib/active_record/attribute_methods/read.rb:40
|
168
|
-
Method: user_id line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
|
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: :name, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
|
173
|
+
Method: :_read_attribute, line: /RUBY_PATH/gems/2.6.0/gems/activerecord-5.2.0/lib/active_record/attribute_methods/read.rb:40
|
174
|
+
Method: :user_id, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
|
169
175
|
.......
|
170
176
|
|
171
177
|
# versus
|
172
178
|
|
173
|
-
Method: name line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
|
174
|
-
Method: user_id line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
|
175
|
-
Method: to_param line: /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
|
179
|
+
Method: :name, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
|
180
|
+
Method: :user_id, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
|
181
|
+
Method: :to_param, line: /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
|
176
182
|
```
|
177
183
|
|
178
184
|
|
@@ -200,7 +206,8 @@ class PostsController < ApplicationController
|
|
200
206
|
|
201
207
|
def show
|
202
208
|
tap_on!(@post) do |payload|
|
203
|
-
|
209
|
+
# this equals to `"Method: #{payload[:method_name]} line: #{payload[:filepath]}:#{payload[:line_number]}"`
|
210
|
+
puts(payload.method_name_and_location)
|
204
211
|
end
|
205
212
|
end
|
206
213
|
end
|
@@ -209,25 +216,25 @@ end
|
|
209
216
|
And you can see these in log:
|
210
217
|
|
211
218
|
```
|
212
|
-
Method: name line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
|
213
|
-
Method: user_id line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
|
214
|
-
Method: to_param line: /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
|
219
|
+
Method: :name, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:5
|
220
|
+
Method: :user_id, line: /PROJECT_PATH/sample/app/views/posts/show.html.erb:10
|
221
|
+
Method: :to_param, line: /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:236
|
215
222
|
```
|
216
223
|
|
217
224
|
### `tap_assoc!`
|
218
225
|
|
219
226
|
```ruby
|
220
227
|
tap_assoc!(order) do |payload|
|
221
|
-
puts
|
228
|
+
puts(payload.method_name_and_location)
|
222
229
|
end
|
223
230
|
```
|
224
231
|
|
225
232
|
```
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
233
|
+
Method: :payments, line: /RUBY_PATH/gems/2.6.0/gems/jsonapi-resources-0.9.10/lib/jsonapi/resource.rb:124
|
234
|
+
Method: :line_items, line: /MY_PROJECT/app/models/line_item_container_helpers.rb:44
|
235
|
+
Method: :effective_line_items, line: /MY_PROJECT/app/models/line_item_container_helpers.rb:110
|
236
|
+
Method: :amending_orders, line: /MY_PROJECT/app/models/order.rb:385
|
237
|
+
Method: :amends_order, line: /MY_PROJECT/app/models/order.rb:432
|
231
238
|
```
|
232
239
|
|
233
240
|
### `tap_sql!` (beta)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class TappingDevice
|
2
|
+
class Payload < Hash
|
3
|
+
ATTRS = [:receiver, :method_name, :arguments, :return_value, :filepath, :line_number, :defined_class, :trace, :tp]
|
4
|
+
|
5
|
+
ATTRS.each do |attr|
|
6
|
+
define_method attr do
|
7
|
+
self[attr]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.init(hash)
|
12
|
+
h = new
|
13
|
+
hash.each do |k, v|
|
14
|
+
h[k] = v
|
15
|
+
end
|
16
|
+
h
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_name_and_location
|
20
|
+
"Method: :#{method_name}, line: #{filepath}:#{line_number}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_name_and_arguments
|
24
|
+
"Method: :#{method_name}, argments: #{arguments.to_s}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/tapping_device.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "active_record"
|
2
2
|
require "tapping_device/version"
|
3
|
+
require "tapping_device/payload"
|
3
4
|
require "tapping_device/trackable"
|
4
5
|
require "tapping_device/exceptions"
|
5
6
|
require "tapping_device/sql_tapping_methods"
|
@@ -152,7 +153,7 @@ class TappingDevice
|
|
152
153
|
def build_yield_parameters(tp:, filepath:, line_number:)
|
153
154
|
arguments = tp.binding.local_variables.map { |n| [n, tp.binding.local_variable_get(n)] }
|
154
155
|
|
155
|
-
{
|
156
|
+
Payload.init({
|
156
157
|
receiver: tp.self,
|
157
158
|
method_name: tp.callee_id,
|
158
159
|
arguments: arguments,
|
@@ -162,7 +163,7 @@ class TappingDevice
|
|
162
163
|
defined_class: tp.defined_class,
|
163
164
|
trace: caller[CALLER_START_POINT..(CALLER_START_POINT + options[:with_trace_to])],
|
164
165
|
tp: tp
|
165
|
-
}
|
166
|
+
})
|
166
167
|
end
|
167
168
|
|
168
169
|
def tap_init?(klass, parameters)
|
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.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- st0012
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- bin/setup
|
131
131
|
- lib/tapping_device.rb
|
132
132
|
- lib/tapping_device/exceptions.rb
|
133
|
+
- lib/tapping_device/payload.rb
|
133
134
|
- lib/tapping_device/sql_listener.rb
|
134
135
|
- lib/tapping_device/sql_tapping_methods.rb
|
135
136
|
- lib/tapping_device/trackable.rb
|