is_this_used 0.1.3 → 0.1.7

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: 30573cf11b6fec940b4b6f03bc541fa36af22d3188e29e82a5ed7e13233a8ce2
4
- data.tar.gz: f6656c296f1edcfe87e9929e1225e0f2b558aa2c92f87d6e5369d705fb70cb9e
3
+ metadata.gz: 777036cad1719ed293fa2d1807dde0268776149b3d88ffe70e9d1b80a0666eb7
4
+ data.tar.gz: c837438870b48369ebbd6378f61329c9685225382c3289df5b25b62ab0e1f926
5
5
  SHA512:
6
- metadata.gz: 51feda93ddd637a4f7f18ea285ff2cc989cb9457c2c3e6cbad1f0b7a0cf6f140e087f3191e25c9bd4b3e326346c12fdb6476cfbce67bf14543bd6042afd7548c
7
- data.tar.gz: e4d69c95977e97d6aafd7aa323af7eb40df7735d969f273a4c122777184c30bfe56799a90ddcffc1036294e2d6831571c9b57df449e0d471fb48b058fcee96d7
6
+ metadata.gz: e1d08ac4789a1208b60cb63f242b26dcf4f79a07cfec9d79bcf9c33d8a30dc20b3b8dcecba5437cee963ea6234d139babb114ceecf406ff8425f50d6207d7022
7
+ data.tar.gz: 6b4c8f9f86fa53873c02ee650327aa2c824408c92560e9b952b44d3f59c40b62346b72420f742e23a576fbaaad30d0d9d1fd9ddebd822c5dbdd1910e0e8fc385
data/Dockerfile ADDED
@@ -0,0 +1,24 @@
1
+ FROM ruby:2.7.5-slim-bullseye
2
+
3
+ WORKDIR /app
4
+
5
+ RUN apt-get update -qq \
6
+ && apt-get install --no-install-recommends -y \
7
+ less \
8
+ nano \
9
+ mariadb-client-10.5 \
10
+ libmariadb-dev-compat \
11
+ libmariadb-dev \
12
+ build-essential \
13
+ git \
14
+ shared-mime-info
15
+
16
+ ARG BUNDLER_VERSION=2.1.4
17
+
18
+ ENV BUNDLE_PATH /bundle
19
+ ENV GEM_HOME /bundle
20
+
21
+ RUN gem update --system \
22
+ && gem install bundler -v ${BUNDLER_VERSION}
23
+
24
+ CMD /bin/bash
data/README.md CHANGED
@@ -59,7 +59,6 @@ You're unsure if the `some_old_method` method is actually being used. You only n
59
59
  Here's an example:
60
60
 
61
61
  ```ruby
62
-
63
62
  class SomeOldClass
64
63
  include IsThisUsed::CruftTracker
65
64
 
@@ -76,7 +75,7 @@ in the `potential_crufts` table that looks like this:
76
75
 
77
76
  | id | owner_name | method_name | method_type | invocations | created_at | updated_at |
78
77
  | --- | ------------ | --------------- | --------------- | ----------- | ------------------- | ------------------- |
79
- | 1 | SomeOldClass | some_old_method | instance_method | 0 | 2022-01-21 14:07:48 | 2022-01-21 14:07:48 |
78
+ | 1 | SomeOldClass | some_old_method | instance_method | 0 | 2022-01-21 14:07:48 | 2022-01-21 14:07:48 |
80
79
 
81
80
  This is easily accessed using the `IsThisUsed::PotentialCruft` model class.
82
81
 
@@ -89,7 +88,7 @@ The fields are:
89
88
  constants, `IsThisUsed::CruftTracker::INSTANCE_METHOD` and `IsThisUsed::CruftTracker::CLASS_METHOD`.
90
89
  - `invocations` - The number of times the method has been invoked.
91
90
  - `created_at` - The date/time we started tracking the method.
92
- - `updated_at` - The last time this record was updated.
91
+ - `updated_at` - The last time this record was updated. IE: the last time the tracked method was invoked.
93
92
 
94
93
  Looking at this, we can see that the `some_old_method` method has never been invoked. This is nice because it means that
95
94
  you can track uses of methods without changing their behavior. A similar record is created for every method you annotate
@@ -98,10 +97,12 @@ with `is_this_used?`.
98
97
  Assuming your production application eagerly loads classes, you should always have records for potentially crufty
99
98
  methods, even if the class itself is never explicitly used.
100
99
 
101
- So, having annotate the method, you can check this table after a while. If you see that there have been zero invocations
100
+ So, having annotated the method, you can check this table after a while. If you see that there have been zero invocations,
102
101
  you have a reasonably good hint that the method may not actually be used. Of course, you should consider that there are
103
102
  some processes that are not run frequently at all, so this gem isn't a panacea. Think before you delete!
104
103
 
104
+ ### Tracking Stacks
105
+
105
106
  In the case that a method _is_ actually invoked, the `invocations` value is incremented and a record is created in
106
107
  the `potential_cruft_stacks` table for each unique invocation stacktrace. This can be used to determine which methods
107
108
  and blocks are responsible for calling the method and are themselves being used. This is the structure of
@@ -113,11 +114,11 @@ the `potential_cruft_stacks` table:
113
114
  - `stack_hash` - This is an MD5 hash of the stack trace for the method's invocation. This is indexed for speedy lookups
114
115
  of stacks.
115
116
  - `stack` - This is a JSON field that stores an array of hashes (more on this in a sec) that is the stack trace for the
116
- method invocation. You can potentially use this to figure out what other methods and blocks involved in calling the
117
+ method invocation. You can potentially use this to figure out what other methods and blocks are involved in calling the
117
118
  not-actually-crufty method.
118
119
  - `occurrences` - This is the number of times the method has been invoked with exactly the same stack.
119
120
  - `created_at` - The date/time we first saw this stack.
120
- - `updated_at` - The last time this saw this stack.
121
+ - `updated_at` - The last time we saw this stack.
121
122
 
122
123
  As a note, if any of the files referenced in the stack are edited sufficiently to change line numbers, the stack will be
123
124
  different and a new record will be created.
@@ -146,7 +147,58 @@ The `label` and `base_label` fields come from Ruby's `Thread::Backtrace::Locatio
146
147
  difference is, as the docs simply say this about `base_label`: "Usually same as label, without decoration". 🤷 Anyhow,
147
148
  it's there if you need it.
148
149
 
149
- The `IsThisUsed::PotentialCruftStack` model is handy for accessing this data.
150
+ ### Tracking Arguments
151
+
152
+ In addition to tracking stacks, you can track details about arguments provided to tracked methods. For example, let's say you have the following method:
153
+
154
+ ```ruby
155
+ def some_old_method(arg1, arg2)
156
+ # do things
157
+ end
158
+ ```
159
+
160
+ Let's say that, for some reason you want to know what arguments are provided to this method. You could add `track_arguments: true` to your `is_this_used?` invocation like so:
161
+
162
+ ```ruby
163
+ is_this_used? :some_old_method, track_arguments: true
164
+ ```
165
+
166
+ Now, as `some_old_method` is invoked, a record will be created in `potential_cruft_arguments` for each unique set of arguments. Similar to `potential_cruft_stacks`, the record contains a hash of the JSON-serialized arguments, the JSON-serialized arguments, and the number of occurrences of the particular combination of arguments.
167
+
168
+ Tracking all arguments might be a really bad idea. Let's say your method actually gets invoked a lot and receives lots of different combinations of arguments, you could be writing a lot of potentially useless information into the `potential_cruft_arguments` table. For this reason arguments are not tracked by default.
169
+
170
+ Instead of tracking all arguments, you can also provide a lambda to `track_arguments` to track only specific details about arguments. The lambda will receive an array of arguments that were provided to the tracked method. Whatever is returned from the lambda is what is tracked in the `potential_cruft_arguments` table. This might be useful in a situation where you have a method that receives an options hash. Maybe you want to know what keys are in that options hash. You could track the unique combination of keys like this:
171
+
172
+ ```ruby
173
+ def ye_olde_method(some_argument, options_hash)
174
+ # do things
175
+ end
176
+
177
+ is_this_used? :some_old_method, track_arguments: ->(args) { args.last.keys.sort }
178
+ ```
179
+
180
+ As `ye_olde_method` is invoked is_this_used will track the unique combination of keys in the `options_hash`. Let's say it's invoked as follows:
181
+
182
+
183
+ ```ruby
184
+ ye_olde_method("Fred", favorite_color: "blue", locality: 'Antartica')
185
+ ye_olde_method("Zelda", locality: 'Hyrule', favorite_color: "green")
186
+ ye_olde_method("Korg", color: 'Rebecca Purple', locality: 'Sakaar')
187
+ ye_olde_method("Liz", status: :favorite_person)
188
+ ```
189
+
190
+ The above would result in the following records in `potential_cruft_arguments` (ignoring the id, potential cruft reference, and timestamps):
191
+
192
+ | arguments_hash | arguments | occurrences |
193
+ | -------------------------------- | ------------------------------ | ----------- |
194
+ | d5d98f761a14b1845a74ce3f1a298c98 | ["favorite_color", "locality"] | 2 |
195
+ | 1619ec6af47253461e87ebf1923a8a83 | ["color", "locality"] | 1 |
196
+ | 88c8205498de97d4ef06b249006bb68b | ["status"] | 1 |
197
+
198
+
199
+
200
+
201
+ ## Models
150
202
 
151
203
  ### `IsThisUsed::PotentialCruft`
152
204
 
@@ -158,6 +210,10 @@ invocations of the method, if any.
158
210
  This is a model representing potential cruft stacks. Its `potential_cruft` method provides an association back to the
159
211
  owning potentially-crufty method.
160
212
 
213
+ ### `IsThisUsed::PotentialCruftArgument`
214
+
215
+ This model represents information about arguments provided to a specific `potential_cruft` method. It is conditionally populated when the `track_arguments` method is provided with either true or a lambda.
216
+
161
217
  ## Dependencies
162
218
 
163
219
  * ActiveRecord - ActiveRecord is used to persist information about potentially crufty methods. This gem should happily
@@ -208,6 +264,37 @@ bundle exec appraisal rails-6.0 rake
208
264
 
209
265
  bundle exec appraisal rails-6.1 rake
210
266
 
267
+ ## Developing with Docker
268
+
269
+ A Docker / docker-compose environment is available to simplify development. Assuming you already have Docker installed, you can spin up a MySQL and open a bash console on a container with Ruby installed like this:
270
+
271
+ ```bash
272
+ docker-compose run --rm ruby bash
273
+ ```
274
+
275
+ The MySQL server has its port exposed as 13306. Note that the first time you spin up these containers it may take a moment for mysql to successfully spin up.
276
+
277
+ The gem's source is mapped to `/app`, which is also the working directory.
278
+
279
+ Once you have a bash console open, you can install dependencies with:
280
+
281
+ ```bash
282
+ bundle install
283
+ bundle exec appraisal install
284
+ ```
285
+
286
+ You can copy the provided MySQL DB config file to be the one to use in the test app:
287
+
288
+ ```bash
289
+ cp spec/dummy_app/config/database.mysql.yml spec/dummy_app/config/database.yml
290
+ ```
291
+
292
+ And now you should be able to run tests against whichever version of Rails you wish, like so:
293
+
294
+ ```bash
295
+ bundle exec appraisal rails-6.1 rake
296
+ ```
297
+
211
298
  ## Contributing
212
299
 
213
300
  Bug reports and pull requests are welcome on GitHub at https://github.com/dhughes/is_this_used.
data/Rakefile CHANGED
@@ -1,6 +1,5 @@
1
- require "bundler/gem_tasks"
1
+ # require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
-
4
3
  RSpec::Core::RakeTask.new(:spec)
5
4
 
6
5
  task :default => :spec
@@ -0,0 +1,39 @@
1
+ version: '3'
2
+ services:
3
+ mysql:
4
+ image: mysql:8
5
+ container_name: square-mysql
6
+ command: --default-authentication-plugin=mysql_native_password
7
+ volumes:
8
+ - db_data:/var/lib/mysql
9
+ environment:
10
+ MYSQL_ROOT_PASSWORD: dev
11
+ MYSQL_DATABASE: is_this_used_test
12
+ ports:
13
+ - "13306:3306"
14
+
15
+ ruby:
16
+ build:
17
+ context: .
18
+ dockerfile: ./Dockerfile
19
+ depends_on:
20
+ - mysql
21
+ working_dir: /app
22
+ volumes:
23
+ - .:/app
24
+ - bundle:/bundle
25
+ - ./wait-for-it.sh:/usr/sbin/wait-for-it.sh:ro
26
+ environment:
27
+ - IS_THIS_USED_DATABASE=is_this_used_test
28
+ - IS_THIS_USED_DB_USER=root
29
+ - IS_THIS_USED_DB_HOST=mysql
30
+ - IS_THIS_USED_DB_PASSWORD=dev
31
+ links:
32
+ - mysql
33
+ tty: true
34
+ stdin_open: true
35
+
36
+ volumes:
37
+ db_data:
38
+ bundle:
39
+
@@ -1,49 +1,49 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- is_this_used (0.1.2)
4
+ is_this_used (0.1.5)
5
5
  activerecord (>= 5.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- actioncable (5.2.4.6)
11
- actionpack (= 5.2.4.6)
10
+ actioncable (5.2.6.2)
11
+ actionpack (= 5.2.6.2)
12
12
  nio4r (~> 2.0)
13
13
  websocket-driver (>= 0.6.1)
14
- actionmailer (5.2.4.6)
15
- actionpack (= 5.2.4.6)
16
- actionview (= 5.2.4.6)
17
- activejob (= 5.2.4.6)
14
+ actionmailer (5.2.6.2)
15
+ actionpack (= 5.2.6.2)
16
+ actionview (= 5.2.6.2)
17
+ activejob (= 5.2.6.2)
18
18
  mail (~> 2.5, >= 2.5.4)
19
19
  rails-dom-testing (~> 2.0)
20
- actionpack (5.2.4.6)
21
- actionview (= 5.2.4.6)
22
- activesupport (= 5.2.4.6)
20
+ actionpack (5.2.6.2)
21
+ actionview (= 5.2.6.2)
22
+ activesupport (= 5.2.6.2)
23
23
  rack (~> 2.0, >= 2.0.8)
24
24
  rack-test (>= 0.6.3)
25
25
  rails-dom-testing (~> 2.0)
26
26
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
- actionview (5.2.4.6)
28
- activesupport (= 5.2.4.6)
27
+ actionview (5.2.6.2)
28
+ activesupport (= 5.2.6.2)
29
29
  builder (~> 3.1)
30
30
  erubi (~> 1.4)
31
31
  rails-dom-testing (~> 2.0)
32
32
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
- activejob (5.2.4.6)
34
- activesupport (= 5.2.4.6)
33
+ activejob (5.2.6.2)
34
+ activesupport (= 5.2.6.2)
35
35
  globalid (>= 0.3.6)
36
- activemodel (5.2.4.6)
37
- activesupport (= 5.2.4.6)
38
- activerecord (5.2.4.6)
39
- activemodel (= 5.2.4.6)
40
- activesupport (= 5.2.4.6)
36
+ activemodel (5.2.6.2)
37
+ activesupport (= 5.2.6.2)
38
+ activerecord (5.2.6.2)
39
+ activemodel (= 5.2.6.2)
40
+ activesupport (= 5.2.6.2)
41
41
  arel (>= 9.0)
42
- activestorage (5.2.4.6)
43
- actionpack (= 5.2.4.6)
44
- activerecord (= 5.2.4.6)
45
- marcel (~> 0.3.1)
46
- activesupport (5.2.4.6)
42
+ activestorage (5.2.6.2)
43
+ actionpack (= 5.2.6.2)
44
+ activerecord (= 5.2.6.2)
45
+ marcel (~> 1.0.0)
46
+ activesupport (5.2.6.2)
47
47
  concurrent-ruby (~> 1.0, >= 1.0.2)
48
48
  i18n (>= 0.7, < 2)
49
49
  minitest (~> 5.1)
@@ -66,27 +66,25 @@ GEM
66
66
  railties (>= 3.0.0)
67
67
  globalid (1.0.0)
68
68
  activesupport (>= 5.0)
69
- i18n (1.8.11)
69
+ i18n (1.10.0)
70
70
  concurrent-ruby (~> 1.0)
71
- loofah (2.13.0)
71
+ loofah (2.14.0)
72
72
  crass (~> 1.0.2)
73
73
  nokogiri (>= 1.5.9)
74
74
  mail (2.7.1)
75
75
  mini_mime (>= 0.1.1)
76
- marcel (0.3.3)
77
- mimemagic (~> 0.3.2)
76
+ marcel (1.0.2)
78
77
  method_source (1.0.0)
79
- mimemagic (0.3.10)
80
- nokogiri (~> 1)
81
- rake
82
78
  mini_mime (1.1.2)
79
+ mini_portile2 (2.8.0)
83
80
  minitest (5.15.0)
84
81
  mysql2 (0.5.3)
85
82
  nio4r (2.5.8)
86
- nokogiri (1.13.1-x86_64-darwin)
83
+ nokogiri (1.13.3)
84
+ mini_portile2 (~> 2.8.0)
87
85
  racc (~> 1.4)
88
86
  parallel (1.21.0)
89
- parser (3.1.0.0)
87
+ parser (3.1.1.0)
90
88
  ast (~> 2.4.1)
91
89
  pry (0.13.1)
92
90
  coderay (~> 1.1)
@@ -98,47 +96,47 @@ GEM
98
96
  rack (2.2.3)
99
97
  rack-test (1.1.0)
100
98
  rack (>= 1.0, < 3)
101
- rails (5.2.4.6)
102
- actioncable (= 5.2.4.6)
103
- actionmailer (= 5.2.4.6)
104
- actionpack (= 5.2.4.6)
105
- actionview (= 5.2.4.6)
106
- activejob (= 5.2.4.6)
107
- activemodel (= 5.2.4.6)
108
- activerecord (= 5.2.4.6)
109
- activestorage (= 5.2.4.6)
110
- activesupport (= 5.2.4.6)
99
+ rails (5.2.6.2)
100
+ actioncable (= 5.2.6.2)
101
+ actionmailer (= 5.2.6.2)
102
+ actionpack (= 5.2.6.2)
103
+ actionview (= 5.2.6.2)
104
+ activejob (= 5.2.6.2)
105
+ activemodel (= 5.2.6.2)
106
+ activerecord (= 5.2.6.2)
107
+ activestorage (= 5.2.6.2)
108
+ activesupport (= 5.2.6.2)
111
109
  bundler (>= 1.3.0)
112
- railties (= 5.2.4.6)
110
+ railties (= 5.2.6.2)
113
111
  sprockets-rails (>= 2.0.0)
114
112
  rails-dom-testing (2.0.3)
115
113
  activesupport (>= 4.2.0)
116
114
  nokogiri (>= 1.6)
117
115
  rails-html-sanitizer (1.4.2)
118
116
  loofah (~> 2.3)
119
- railties (5.2.4.6)
120
- actionpack (= 5.2.4.6)
121
- activesupport (= 5.2.4.6)
117
+ railties (5.2.6.2)
118
+ actionpack (= 5.2.6.2)
119
+ activesupport (= 5.2.6.2)
122
120
  method_source
123
121
  rake (>= 0.8.7)
124
122
  thor (>= 0.19.0, < 2.0)
125
123
  rainbow (3.1.1)
126
124
  rake (10.5.0)
127
- regexp_parser (2.2.0)
125
+ regexp_parser (2.2.1)
128
126
  rexml (3.2.5)
129
- rspec (3.10.0)
130
- rspec-core (~> 3.10.0)
131
- rspec-expectations (~> 3.10.0)
132
- rspec-mocks (~> 3.10.0)
133
- rspec-core (3.10.1)
134
- rspec-support (~> 3.10.0)
135
- rspec-expectations (3.10.2)
127
+ rspec (3.11.0)
128
+ rspec-core (~> 3.11.0)
129
+ rspec-expectations (~> 3.11.0)
130
+ rspec-mocks (~> 3.11.0)
131
+ rspec-core (3.11.0)
132
+ rspec-support (~> 3.11.0)
133
+ rspec-expectations (3.11.0)
136
134
  diff-lcs (>= 1.2.0, < 2.0)
137
- rspec-support (~> 3.10.0)
138
- rspec-mocks (3.10.2)
135
+ rspec-support (~> 3.11.0)
136
+ rspec-mocks (3.11.0)
139
137
  diff-lcs (>= 1.2.0, < 2.0)
140
- rspec-support (~> 3.10.0)
141
- rspec-rails (5.0.2)
138
+ rspec-support (~> 3.11.0)
139
+ rspec-rails (5.0.3)
142
140
  actionpack (>= 5.2)
143
141
  activesupport (>= 5.2)
144
142
  railties (>= 5.2)
@@ -146,7 +144,7 @@ GEM
146
144
  rspec-expectations (~> 3.10)
147
145
  rspec-mocks (~> 3.10)
148
146
  rspec-support (~> 3.10)
149
- rspec-support (3.10.3)
147
+ rspec-support (3.11.0)
150
148
  rubocop (1.22.3)
151
149
  parallel (~> 1.10)
152
150
  parser (>= 3.0.0.0)
@@ -156,8 +154,8 @@ GEM
156
154
  rubocop-ast (>= 1.12.0, < 2.0)
157
155
  ruby-progressbar (~> 1.7)
158
156
  unicode-display_width (>= 1.4.0, < 3.0)
159
- rubocop-ast (1.15.1)
160
- parser (>= 3.0.1.1)
157
+ rubocop-ast (1.16.0)
158
+ parser (>= 3.1.1.0)
161
159
  rubocop-rails (2.12.4)
162
160
  activesupport (>= 4.2.0)
163
161
  rack (>= 1.1)
@@ -167,7 +165,7 @@ GEM
167
165
  rubocop-rspec (2.5.0)
168
166
  rubocop (~> 1.19)
169
167
  ruby-progressbar (1.11.0)
170
- sprockets (4.0.2)
168
+ sprockets (4.0.3)
171
169
  concurrent-ruby (~> 1.0)
172
170
  rack (> 1, < 3)
173
171
  sprockets-rails (3.4.2)
@@ -188,7 +186,7 @@ PLATFORMS
188
186
 
189
187
  DEPENDENCIES
190
188
  appraisal (~> 2.4.1)
191
- bundler (~> 1.17)
189
+ bundler (~> 2.1)
192
190
  generator_spec (~> 0.9.4)
193
191
  is_this_used!
194
192
  mysql2 (~> 0.5.3)
@@ -203,4 +201,4 @@ DEPENDENCIES
203
201
  rubocop-rspec (~> 2.5.0)
204
202
 
205
203
  BUNDLED WITH
206
- 1.17.2
204
+ 2.1.4