is_this_used 0.1.0 → 0.1.4

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: 6230a093e9c43e76f49f6029109cb16fa0287deb22285e1f01b0d0732fe076dc
4
- data.tar.gz: 3e8d0cd036daec5d0feb1a0d17fe94f0a0e1509f24962145ca01dedded5e5ea1
3
+ metadata.gz: d781ed2774ddda32adf7b68b87cc0666e6c08de682b07ab2fbec2daa45f02789
4
+ data.tar.gz: fd32bc27fa9ece52099e5cc1214394c14753478c7677c70ab04e4b31ca92a3f6
5
5
  SHA512:
6
- metadata.gz: ccd634c5a840b1c0aa82ac266dda18e6da727e4b65f31829b81ca549eb792e0ae866aaf91eca93d45107149b8b28f8d760c900e95eb174101f5a1f2a8a4e3e80
7
- data.tar.gz: 5e1b79de772247fefc0304d75346f507d9de0f88533808a715f3fb2f38057c6703dff0b28ecf83bedfb253b3fb96bd6384ccb0805b2c1d8bb46f2999de661dba
6
+ metadata.gz: 9da3a8cfda9bc44da06b7732e205d410709a2166568cbaacd8c17020b2960f40cfd2043ea82cd6dc96eb8a37aa7c08ae616669683cde524087b88f6f083fdb3c
7
+ data.tar.gz: 8c0ae318620b3808a775c9c9d9301bc942d4426dea9981eba78433e5055d1b1484ddc717b836afdd9b9b12a5921b81cba39d839ee3bb3cb46af8d7a29600b4a9
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ *.gem
1
2
  /.bundle/
2
3
  /.yardoc
3
4
  /_yardoc/
data/Appraisals CHANGED
@@ -18,7 +18,6 @@ appraise "rails-6.0" do
18
18
  gem "rails", "~> 6.0.3"
19
19
  end
20
20
 
21
- # appraise "rails-6.1" do
22
- # gem "rails", "~> 6.1.0"
23
- # gem "rails-controller-testing", "~> 1.0.5"
24
- # end
21
+ appraise "rails-6.1" do
22
+ gem "rails", "~> 6.1.0"
23
+ end
data/README.md CHANGED
@@ -1,16 +1,11 @@
1
- # IsThisUsed
1
+ Have you ever asked yourself, "Is this method even being used?!" Does your application use ActiveRecord? If the answers
2
+ to these two questions are yes, this gem may be of use to you!
2
3
 
3
- Do you ever ask yourself, "Is this method even being used?!" Does your app use ActiveRecord? If so, this gem may be
4
- useful to you!
4
+ Large applications can accrue cruft; old methods that might once have been important, but are now unused. Unfortunately,
5
+ software is _complex_ and sometimes it's unclear if a given method is still being used at all. This adds maintenance
6
+ burdens, headaches, and uncertainty.
5
7
 
6
- Large applications can accrue cruft; old methods that might once have been important, but are now unused. The thing is,
7
- software is _complex_. If the method in question was a part of a larger feature that has changed over time, or possibly
8
- been removed or disabled, it can be really difficult to tell if it's being used at all. It can be even worse that than.
9
- The method could be _tested_. 😨 That's bad because now you can tell if you break it and you'll need to maintain it, but
10
- you have no idea if there's any value in the time and effort to do so.
11
-
12
- The gem uses ActiveRecord to write to your database, so that's a dependency. Basically, this is really only useful for
13
- Rails applications at this point. Also, as of now, only MySQL is supported. Other DBs could be added in the future.
8
+ This gem aims to give you a couple tools to make it easier to know whether specific methods are being used, or not.
14
9
 
15
10
  ## Installation
16
11
 
@@ -36,7 +31,7 @@ You'll need to generate and run the migrations to add the required tables to you
36
31
 
37
32
  ```bash
38
33
  bundle exec rails generate is_this_used:migration
39
- bundle exec rails db:migrate
34
+ bundle exec rails db:migrate
40
35
  ```
41
36
 
42
37
  This will create two tables, `potential_crufts`, which is used to collect information on the methods you're
@@ -45,31 +40,183 @@ actually occur.
45
40
 
46
41
  ## Usage
47
42
 
48
- TODO
43
+ is_this_used is pretty simple. Let's say you have a class (or module) like this...
44
+
45
+ ```ruby
46
+
47
+ class SomeOldClass
48
+ def some_old_method
49
+ # do things
50
+ end
51
+ end
52
+ ```
53
+
54
+ You're unsure if the `some_old_method` method is actually being used. You only need to do two things:
55
+
56
+ 1. include the `IsThisUsed::CruftTracker` module
57
+ 2. use the `is_this_used?` method and name the method in question
58
+
59
+ Here's an example:
60
+
61
+ ```ruby
62
+
63
+ class SomeOldClass
64
+ include IsThisUsed::CruftTracker
65
+
66
+ def some_old_method
67
+ # do things
68
+ end
69
+
70
+ is_this_used? :some_old_method
71
+ end
72
+ ```
73
+
74
+ What do you get out of this? Well, as soon as Ruby loads the `SomeOldClass` class, is_this_used will create a new record
75
+ in the `potential_crufts` table that looks like this:
76
+
77
+ | id | owner_name | method_name | method_type | invocations | created_at | updated_at |
78
+ | --- | ------------ | --------------- | --------------- | ----------- | ------------------- | ------------------- |
79
+ | 1 | SomeOldClass | some_old_method | instance_method | 0 | 2022-01-21 14:07:48 | 2022-01-21 14:07:48 |
80
+
81
+ This is easily accessed using the `IsThisUsed::PotentialCruft` model class.
82
+
83
+ The fields are:
84
+
85
+ - `id` - Shockingly, this is the primary key.
86
+ - `owner_name` - This is the name of the Ruby class or module that owns the method.
87
+ - `method_name` - This is the name of the method.
88
+ - `method_type` - This is either "instance_method" or "class_method", which are the values of the corresponding
89
+ constants, `IsThisUsed::CruftTracker::INSTANCE_METHOD` and `IsThisUsed::CruftTracker::CLASS_METHOD`.
90
+ - `invocations` - The number of times the method has been invoked.
91
+ - `created_at` - The date/time we started tracking the method.
92
+ - `updated_at` - The last time this record was updated.
93
+
94
+ Looking at this, we can see that the `some_old_method` method has never been invoked. This is nice because it means that
95
+ you can track uses of methods without changing their behavior. A similar record is created for every method you annotate
96
+ with `is_this_used?`.
97
+
98
+ Assuming your production application eagerly loads classes, you should always have records for potentially crufty
99
+ methods, even if the class itself is never explicitly used.
100
+
101
+ So, having annotate the method, you can check this table after a while. If you see that there have been zero invocations
102
+ you have a reasonably good hint that the method may not actually be used. Of course, you should consider that there are
103
+ some processes that are not run frequently at all, so this gem isn't a panacea. Think before you delete!
104
+
105
+ In the case that a method _is_ actually invoked, the `invocations` value is incremented and a record is created in
106
+ the `potential_cruft_stacks` table for each unique invocation stacktrace. This can be used to determine which methods
107
+ and blocks are responsible for calling the method and are themselves being used. This is the structure of
108
+ the `potential_cruft_stacks` table:
109
+
110
+ - `id` - You might be surprised to learn this is the primary key.
111
+ - `potential_cruft_id` - A reference to the `potential_crufts` record for the not-actually-crufty method that was
112
+ invoked.
113
+ - `stack_hash` - This is an MD5 hash of the stack trace for the method's invocation. This is indexed for speedy lookups
114
+ of stacks.
115
+ - `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
+ not-actually-crufty method.
118
+ - `occurrences` - This is the number of times the method has been invoked with exactly the same stack.
119
+ - `created_at` - The date/time we first saw this stack.
120
+ - `updated_at` - The last time this saw this stack.
121
+
122
+ As a note, if any of the files referenced in the stack are edited sufficiently to change line numbers, the stack will be
123
+ different and a new record will be created.
124
+
125
+ The `stack` data looks like this:
126
+
127
+ ```javascript
128
+ [
129
+ {
130
+ path: "/app/path/to/some_file.rb",
131
+ label: "some_method",
132
+ lineno: 2056,
133
+ base_label: "some_method",
134
+ },
135
+ {
136
+ path: "/app/another/path/to/a_file.rb",
137
+ label: "do_it",
138
+ lineno: 2125,
139
+ base_label: "do_it",
140
+ },
141
+ // ...
142
+ ]
143
+ ```
144
+
145
+ The `label` and `base_label` fields come from Ruby's `Thread::Backtrace::Location`. I'm not actually sure what the
146
+ difference is, as the docs simply say this about `base_label`: "Usually same as label, without decoration". 🤷 Anyhow,
147
+ it's there if you need it.
148
+
149
+ The `IsThisUsed::PotentialCruftStack` model is handy for accessing this data.
150
+
151
+ ### `IsThisUsed::PotentialCruft`
152
+
153
+ This is a model representing the potential cruft. You can use its `potential_cruft_stacks` to get a list of all of the
154
+ invocations of the method, if any.
155
+
156
+ ### `IsThisUsed::PotentialCruftStack`
157
+
158
+ This is a model representing potential cruft stacks. Its `potential_cruft` method provides an association back to the
159
+ owning potentially-crufty method.
160
+
161
+ ## Dependencies
162
+
163
+ * ActiveRecord - ActiveRecord is used to persist information about potentially crufty methods. This gem should happily
164
+ work with AR 5.2, 6, and 6.1.
165
+ * MySQL - As of now, only MySQL is supported. PRs are welcome to add support for Postgres, etc.
49
166
 
50
167
  ## Development
51
168
 
52
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
53
- also run `bin/console` for an interactive prompt that will allow you to experiment.
169
+ This gem is _highly_ inspired by [PaperTrail](https://github.com/paper-trail-gem/paper_trail). What this means is that
170
+ the entire approach to development and a lot of testing setup and support code is essentially copy and pasted from
171
+ PaperTrail. A huge debt of gratitude is owed to the maintainers of PaperTrail. Thus, if anything below is too vague,
172
+ it'd probably be helpful to
173
+ see [what PaperTrail has to say about developing their gem](https://github.com/paper-trail-gem/paper_trail/blob/master/.github/CONTRIBUTING.md)
174
+ .
54
175
 
55
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
56
- version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
57
- push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
176
+ Anyhow, to get started:
58
177
 
59
- ## Contributing
178
+ ```bash
179
+ gem install bundler
180
+ bundle
181
+ bundle exec appraisal install
182
+ bundle exec appraisal update # occasionally
183
+ ```
60
184
 
61
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/is_this_used.
185
+ Development may be a bit awkward because the test suite supports a few versions of Rails (5.2, 6, and 6.1) and contains
186
+ a dummy application that depends on MySQL.
62
187
 
63
- ## License
188
+ Before running the tests you'll want to make sure you have a database.yml file:
64
189
 
65
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
190
+ ```bash
191
+ cd spec/dummy_app
192
+ cp config/database.mysql.yml config/database.yml
193
+ ```
194
+
195
+ Edit the database.yml as needed and fire up your MySQL server.
66
196
 
67
- # To run tests
197
+ You can now run the test suite:
68
198
 
69
- ## Rails 5.2
199
+ ### Rails 5.2
70
200
 
71
201
  bundle exec appraisal rails-5.2 rake
72
202
 
73
- ## Rails 6.0
203
+ ### Rails 6.0
74
204
 
75
205
  bundle exec appraisal rails-6.0 rake
206
+
207
+ ### Rails 6.1
208
+
209
+ bundle exec appraisal rails-6.1 rake
210
+
211
+ ## Contributing
212
+
213
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dhughes/is_this_used.
214
+
215
+ ## Inspirations
216
+
217
+ Large quantities of the approach taken to testing and general setup were gratuitously "borrowed"
218
+ from [PaperTrail](https://github.com/paper-trail-gem/paper_trail). Thank you!
219
+
220
+ ## License
221
+
222
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -3,6 +3,5 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 5.2.4"
6
- # gem "rails-controller-testing", "~> 1.0.2"
7
6
 
8
7
  gemspec path: "../"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- is_this_used (0.1.0)
4
+ is_this_used (0.1.4)
5
5
  activerecord (>= 5.2)
6
6
 
7
7
  GEM
@@ -3,6 +3,5 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 6.0.3"
6
- # gem "rails-controller-testing", "~> 1.0.3"
7
6
 
8
7
  gemspec path: "../"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- is_this_used (0.1.0)
4
+ is_this_used (0.1.4)
5
5
  activerecord (>= 5.2)
6
6
 
7
7
  GEM
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 6.1.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,221 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ is_this_used (0.1.4)
5
+ activerecord (>= 5.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (6.1.4.4)
11
+ actionpack (= 6.1.4.4)
12
+ activesupport (= 6.1.4.4)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailbox (6.1.4.4)
16
+ actionpack (= 6.1.4.4)
17
+ activejob (= 6.1.4.4)
18
+ activerecord (= 6.1.4.4)
19
+ activestorage (= 6.1.4.4)
20
+ activesupport (= 6.1.4.4)
21
+ mail (>= 2.7.1)
22
+ actionmailer (6.1.4.4)
23
+ actionpack (= 6.1.4.4)
24
+ actionview (= 6.1.4.4)
25
+ activejob (= 6.1.4.4)
26
+ activesupport (= 6.1.4.4)
27
+ mail (~> 2.5, >= 2.5.4)
28
+ rails-dom-testing (~> 2.0)
29
+ actionpack (6.1.4.4)
30
+ actionview (= 6.1.4.4)
31
+ activesupport (= 6.1.4.4)
32
+ rack (~> 2.0, >= 2.0.9)
33
+ rack-test (>= 0.6.3)
34
+ rails-dom-testing (~> 2.0)
35
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
36
+ actiontext (6.1.4.4)
37
+ actionpack (= 6.1.4.4)
38
+ activerecord (= 6.1.4.4)
39
+ activestorage (= 6.1.4.4)
40
+ activesupport (= 6.1.4.4)
41
+ nokogiri (>= 1.8.5)
42
+ actionview (6.1.4.4)
43
+ activesupport (= 6.1.4.4)
44
+ builder (~> 3.1)
45
+ erubi (~> 1.4)
46
+ rails-dom-testing (~> 2.0)
47
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
48
+ activejob (6.1.4.4)
49
+ activesupport (= 6.1.4.4)
50
+ globalid (>= 0.3.6)
51
+ activemodel (6.1.4.4)
52
+ activesupport (= 6.1.4.4)
53
+ activerecord (6.1.4.4)
54
+ activemodel (= 6.1.4.4)
55
+ activesupport (= 6.1.4.4)
56
+ activestorage (6.1.4.4)
57
+ actionpack (= 6.1.4.4)
58
+ activejob (= 6.1.4.4)
59
+ activerecord (= 6.1.4.4)
60
+ activesupport (= 6.1.4.4)
61
+ marcel (~> 1.0.0)
62
+ mini_mime (>= 1.1.0)
63
+ activesupport (6.1.4.4)
64
+ concurrent-ruby (~> 1.0, >= 1.0.2)
65
+ i18n (>= 1.6, < 2)
66
+ minitest (>= 5.1)
67
+ tzinfo (~> 2.0)
68
+ zeitwerk (~> 2.3)
69
+ appraisal (2.4.1)
70
+ bundler
71
+ rake
72
+ thor (>= 0.14.0)
73
+ ast (2.4.2)
74
+ builder (3.2.4)
75
+ byebug (11.1.3)
76
+ coderay (1.1.3)
77
+ concurrent-ruby (1.1.9)
78
+ crass (1.0.6)
79
+ diff-lcs (1.5.0)
80
+ erubi (1.10.0)
81
+ generator_spec (0.9.4)
82
+ activesupport (>= 3.0.0)
83
+ railties (>= 3.0.0)
84
+ globalid (1.0.0)
85
+ activesupport (>= 5.0)
86
+ i18n (1.8.11)
87
+ concurrent-ruby (~> 1.0)
88
+ loofah (2.13.0)
89
+ crass (~> 1.0.2)
90
+ nokogiri (>= 1.5.9)
91
+ mail (2.7.1)
92
+ mini_mime (>= 0.1.1)
93
+ marcel (1.0.2)
94
+ method_source (1.0.0)
95
+ mini_mime (1.1.2)
96
+ minitest (5.15.0)
97
+ mysql2 (0.5.3)
98
+ nio4r (2.5.8)
99
+ nokogiri (1.13.1-x86_64-darwin)
100
+ racc (~> 1.4)
101
+ parallel (1.21.0)
102
+ parser (3.1.0.0)
103
+ ast (~> 2.4.1)
104
+ pry (0.13.1)
105
+ coderay (~> 1.1)
106
+ method_source (~> 1.0)
107
+ pry-byebug (3.9.0)
108
+ byebug (~> 11.0)
109
+ pry (~> 0.13.0)
110
+ racc (1.6.0)
111
+ rack (2.2.3)
112
+ rack-test (1.1.0)
113
+ rack (>= 1.0, < 3)
114
+ rails (6.1.4.4)
115
+ actioncable (= 6.1.4.4)
116
+ actionmailbox (= 6.1.4.4)
117
+ actionmailer (= 6.1.4.4)
118
+ actionpack (= 6.1.4.4)
119
+ actiontext (= 6.1.4.4)
120
+ actionview (= 6.1.4.4)
121
+ activejob (= 6.1.4.4)
122
+ activemodel (= 6.1.4.4)
123
+ activerecord (= 6.1.4.4)
124
+ activestorage (= 6.1.4.4)
125
+ activesupport (= 6.1.4.4)
126
+ bundler (>= 1.15.0)
127
+ railties (= 6.1.4.4)
128
+ sprockets-rails (>= 2.0.0)
129
+ rails-dom-testing (2.0.3)
130
+ activesupport (>= 4.2.0)
131
+ nokogiri (>= 1.6)
132
+ rails-html-sanitizer (1.4.2)
133
+ loofah (~> 2.3)
134
+ railties (6.1.4.4)
135
+ actionpack (= 6.1.4.4)
136
+ activesupport (= 6.1.4.4)
137
+ method_source
138
+ rake (>= 0.13)
139
+ thor (~> 1.0)
140
+ rainbow (3.1.1)
141
+ rake (10.5.0)
142
+ regexp_parser (2.2.0)
143
+ rexml (3.2.5)
144
+ rspec (3.10.0)
145
+ rspec-core (~> 3.10.0)
146
+ rspec-expectations (~> 3.10.0)
147
+ rspec-mocks (~> 3.10.0)
148
+ rspec-core (3.10.1)
149
+ rspec-support (~> 3.10.0)
150
+ rspec-expectations (3.10.2)
151
+ diff-lcs (>= 1.2.0, < 2.0)
152
+ rspec-support (~> 3.10.0)
153
+ rspec-mocks (3.10.2)
154
+ diff-lcs (>= 1.2.0, < 2.0)
155
+ rspec-support (~> 3.10.0)
156
+ rspec-rails (5.0.2)
157
+ actionpack (>= 5.2)
158
+ activesupport (>= 5.2)
159
+ railties (>= 5.2)
160
+ rspec-core (~> 3.10)
161
+ rspec-expectations (~> 3.10)
162
+ rspec-mocks (~> 3.10)
163
+ rspec-support (~> 3.10)
164
+ rspec-support (3.10.3)
165
+ rubocop (1.22.3)
166
+ parallel (~> 1.10)
167
+ parser (>= 3.0.0.0)
168
+ rainbow (>= 2.2.2, < 4.0)
169
+ regexp_parser (>= 1.8, < 3.0)
170
+ rexml
171
+ rubocop-ast (>= 1.12.0, < 2.0)
172
+ ruby-progressbar (~> 1.7)
173
+ unicode-display_width (>= 1.4.0, < 3.0)
174
+ rubocop-ast (1.15.1)
175
+ parser (>= 3.0.1.1)
176
+ rubocop-rails (2.12.4)
177
+ activesupport (>= 4.2.0)
178
+ rack (>= 1.1)
179
+ rubocop (>= 1.7.0, < 2.0)
180
+ rubocop-rake (0.6.0)
181
+ rubocop (~> 1.0)
182
+ rubocop-rspec (2.5.0)
183
+ rubocop (~> 1.19)
184
+ ruby-progressbar (1.11.0)
185
+ sprockets (4.0.2)
186
+ concurrent-ruby (~> 1.0)
187
+ rack (> 1, < 3)
188
+ sprockets-rails (3.4.2)
189
+ actionpack (>= 5.2)
190
+ activesupport (>= 5.2)
191
+ sprockets (>= 3.0.0)
192
+ thor (1.2.1)
193
+ tzinfo (2.0.4)
194
+ concurrent-ruby (~> 1.0)
195
+ unicode-display_width (2.1.0)
196
+ websocket-driver (0.7.5)
197
+ websocket-extensions (>= 0.1.0)
198
+ websocket-extensions (0.1.5)
199
+ zeitwerk (2.5.3)
200
+
201
+ PLATFORMS
202
+ ruby
203
+
204
+ DEPENDENCIES
205
+ appraisal (~> 2.4.1)
206
+ bundler (~> 1.17)
207
+ generator_spec (~> 0.9.4)
208
+ is_this_used!
209
+ mysql2 (~> 0.5.3)
210
+ pry-byebug (~> 3.9)
211
+ rails (~> 6.1.0)
212
+ rake (~> 10.0)
213
+ rspec (~> 3.0)
214
+ rspec-rails (~> 5.0.2)
215
+ rubocop (~> 1.22.2)
216
+ rubocop-rails (~> 2.12.4)
217
+ rubocop-rake (~> 0.6.0)
218
+ rubocop-rspec (~> 2.5.0)
219
+
220
+ BUNDLED WITH
221
+ 1.17.2
@@ -2,8 +2,6 @@
2
2
 
3
3
  module IsThisUsed
4
4
  module CruftTracker
5
- extend ActiveSupport::Concern
6
-
7
5
  INSTANCE_METHOD = 'instance_method'
8
6
  CLASS_METHOD = 'class_method'
9
7
 
@@ -39,7 +37,9 @@ module IsThisUsed
39
37
 
40
38
  def self.filtered_stack
41
39
  caller_locations.reject do |location|
42
- location.path.match?(%r{\/lib\/is_this_used\/cruft_tracker.rb$})
40
+ location.path.match?(
41
+ %r{\/lib\/is_this_used\/(cruft_tracker.rb|util\/log_suppressor.rb)$}
42
+ )
43
43
  end.map do |location|
44
44
  {
45
45
  path: location.path,
@@ -51,27 +51,44 @@ module IsThisUsed
51
51
  end
52
52
  end
53
53
 
54
- class_methods do
55
- def is_this_used?(method_name, method_type: nil)
56
- method_type ||= determine_method_type(method_name)
57
- target_method = target_method(method_name, method_type)
58
-
59
- potential_cruft =
60
- PotentialCruft.find_or_create_by(
61
- owner_name: self.name,
62
- method_name: method_name,
63
- method_type: method_type
64
- )
65
-
66
- target_method.owner.define_method target_method.name do |*args|
67
- CruftTracker::Recorder.record_invocation(potential_cruft)
54
+ def self.included(base)
55
+ base.extend ClassMethods
56
+ end
68
57
 
69
- if method_type == INSTANCE_METHOD
70
- target_method.bind(self).call(*args)
71
- else
72
- target_method.call(*args)
58
+ module ClassMethods
59
+ def is_this_used?(method_name, method_type: nil)
60
+ IsThisUsed::Util::LogSuppressor.suppress_logging do
61
+ method_type ||= determine_method_type(method_name)
62
+ target_method = target_method(method_name, method_type)
63
+
64
+ potential_cruft =
65
+ PotentialCruft.find_or_create_by(
66
+ owner_name: self.name,
67
+ method_name: method_name,
68
+ method_type: method_type
69
+ )
70
+
71
+ target_method.owner.define_method target_method.name do |*args|
72
+ IsThisUsed::Util::LogSuppressor.suppress_logging do
73
+ CruftTracker::Recorder.record_invocation(potential_cruft)
74
+ end
75
+ if method_type == INSTANCE_METHOD
76
+ target_method.bind(self).call(*args)
77
+ else
78
+ target_method.call(*args)
79
+ end
73
80
  end
74
81
  end
82
+ rescue ActiveRecord::StatementInvalid => e
83
+ raise unless e.cause.present? && e.cause.instance_of?(Mysql2::Error)
84
+
85
+ Rails.logger.warn(
86
+ 'There was an error recording potential cruft. Does the potential_crufts table exist?'
87
+ )
88
+ rescue Mysql2::Error::ConnectionError
89
+ Rails.logger.warn(
90
+ 'There was an error recording potential cruft due to being unable to connect to the database. This may be a non-issue in cases where the database is intentionally not available.'
91
+ )
75
92
  end
76
93
 
77
94
  def target_method(method_name, method_type)
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IsThisUsed
4
+ module Util
5
+ class LogSuppressor
6
+ def self.suppress_logging
7
+ initial_log_level = ActiveRecord::Base.logger.level
8
+ ActiveRecord::Base.logger.level = :error
9
+ yield
10
+ ActiveRecord::Base.logger.level = initial_log_level
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IsThisUsed
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.4'
5
5
  end
data/lib/is_this_used.rb CHANGED
@@ -2,6 +2,7 @@ require 'is_this_used/version'
2
2
  require 'is_this_used/cruft_tracker'
3
3
  require 'is_this_used/models/potential_cruft'
4
4
  require 'is_this_used/models/potential_cruft_stack'
5
+ require 'is_this_used/util/log_suppressor'
5
6
 
6
7
  module IsThisUsed
7
8
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: is_this_used
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Hughes
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-21 00:00:00.000000000 Z
11
+ date: 2022-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -228,6 +228,8 @@ files:
228
228
  - gemfiles/rails_5.2.gemfile.lock
229
229
  - gemfiles/rails_6.0.gemfile
230
230
  - gemfiles/rails_6.0.gemfile.lock
231
+ - gemfiles/rails_6.1.gemfile
232
+ - gemfiles/rails_6.1.gemfile.lock
231
233
  - is_this_used.gemspec
232
234
  - lib/generators/is_this_used/migration_generator.rb
233
235
  - lib/generators/is_this_used/templates/create_potential_cruft_stacks.rb.erb
@@ -236,6 +238,7 @@ files:
236
238
  - lib/is_this_used/cruft_tracker.rb
237
239
  - lib/is_this_used/models/potential_cruft.rb
238
240
  - lib/is_this_used/models/potential_cruft_stack.rb
241
+ - lib/is_this_used/util/log_suppressor.rb
239
242
  - lib/is_this_used/version.rb
240
243
  - log/test.log
241
244
  homepage: https://github.com/dhughes/is_this_used