is_this_used 0.1.7 → 0.1.8

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: 777036cad1719ed293fa2d1807dde0268776149b3d88ffe70e9d1b80a0666eb7
4
- data.tar.gz: c837438870b48369ebbd6378f61329c9685225382c3289df5b25b62ab0e1f926
3
+ metadata.gz: 6d99ea88778f65e0685fa42ec856e9a61b5414ad87e1963f21f8d8941ee62eea
4
+ data.tar.gz: 7f5bf7cb77f3688c14035480a1ed16130998f004a6add05afc125bd18f21b6fa
5
5
  SHA512:
6
- metadata.gz: e1d08ac4789a1208b60cb63f242b26dcf4f79a07cfec9d79bcf9c33d8a30dc20b3b8dcecba5437cee963ea6234d139babb114ceecf406ff8425f50d6207d7022
7
- data.tar.gz: 6b4c8f9f86fa53873c02ee650327aa2c824408c92560e9b952b44d3f59c40b62346b72420f742e23a576fbaaad30d0d9d1fd9ddebd822c5dbdd1910e0e8fc385
6
+ metadata.gz: 5e7acfe828e892998c319ca07bc6bc2ff2bf91c5c2e9e65debbb67f7aacd9c07f9561af8e2fc37dfc140f2ade3d97530fcaf5b9e65a5c13b8a15c60320cc0c92
7
+ data.tar.gz: dc4363da4d9030568766e2d37cf2c932f5b5bf35d8c5b20d6587c45f12f3c4953357048155e9664523059da54e4d3f8e534578ebd9bc76d9e601c55bb46888b6
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Have you ever asked yourself, "Is this method even being used?!" Does your application use ActiveRecord? If the answers
1
+ Have you ever asked yourself, "Is this method even being used?!" Does your application use Rails and ActiveRecord? If the answers
2
2
  to these two questions are yes, this gem may be of use to you!
3
3
 
4
4
  Large applications can accrue cruft; old methods that might once have been important, but are now unused. Unfortunately,
@@ -73,9 +73,9 @@ end
73
73
  What do you get out of this? Well, as soon as Ruby loads the `SomeOldClass` class, is_this_used will create a new record
74
74
  in the `potential_crufts` table that looks like this:
75
75
 
76
- | id | owner_name | method_name | method_type | invocations | created_at | updated_at |
77
- | --- | ------------ | --------------- | --------------- | ----------- | ------------------- | ------------------- |
78
- | 1 | SomeOldClass | some_old_method | instance_method | 0 | 2022-01-21 14:07:48 | 2022-01-21 14:07:48 |
76
+ | id | owner_name | method_name | method_type | invocations | deleted_at | created_at | updated_at |
77
+ | --- | ------------ | --------------- | --------------- | ----------- | ---------- | ------------------- | ------------------- |
78
+ | 1 | SomeOldClass | some_old_method | instance_method | 0 | null | 2022-01-21 14:07:48 | 2022-01-21 14:07:48 |
79
79
 
80
80
  This is easily accessed using the `IsThisUsed::PotentialCruft` model class.
81
81
 
@@ -87,6 +87,7 @@ The fields are:
87
87
  - `method_type` - This is either "instance_method" or "class_method", which are the values of the corresponding
88
88
  constants, `IsThisUsed::CruftTracker::INSTANCE_METHOD` and `IsThisUsed::CruftTracker::CLASS_METHOD`.
89
89
  - `invocations` - The number of times the method has been invoked.
90
+ - `deleted_at` - When set, this indicates that the method is no longer being tracked.
90
91
  - `created_at` - The date/time we started tracking the method.
91
92
  - `updated_at` - The last time this record was updated. IE: the last time the tracked method was invoked.
92
93
 
@@ -216,6 +217,7 @@ This model represents information about arguments provided to a specific `potent
216
217
 
217
218
  ## Dependencies
218
219
 
220
+ * Rails - Versions 5.2, 6, and 6.1 are supported.
219
221
  * ActiveRecord - ActiveRecord is used to persist information about potentially crufty methods. This gem should happily
220
222
  work with AR 5.2, 6, and 6.1.
221
223
  * MySQL - As of now, only MySQL is supported. PRs are welcome to add support for Postgres, etc.
data/Rakefile CHANGED
@@ -1,5 +1,24 @@
1
- # require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
- RSpec::Core::RakeTask.new(:spec)
1
+ # frozen_string_literal: true
4
2
 
5
- task :default => :spec
3
+ require "fileutils"
4
+ require "bundler"
5
+ require './spec/support/is_this_used_spec_migrator'
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ desc "Create the database."
9
+ task :create_db do
10
+ ENV['RAILS_ENV'] = 'test'
11
+ system "mysqladmin -f -h #{ENV.fetch('IS_THIS_USED_DB_HOST', 'localhost')} -P #{ENV.fetch('IS_THIS_USED_DB_PORT', 3306)} -u #{ENV.fetch('IS_THIS_USED_DB_USER', 'root')} --password=#{ENV.fetch('IS_THIS_USED_DB_PASSWORD', 'dev')} drop is_this_used_test"
12
+ system "mysqladmin -h #{ENV.fetch('IS_THIS_USED_DB_HOST', 'localhost')} -P #{ENV.fetch('IS_THIS_USED_DB_PORT', 3306)} -u #{ENV.fetch('IS_THIS_USED_DB_USER', 'root')} --password=#{ENV.fetch('IS_THIS_USED_DB_PASSWORD', 'dev')} create is_this_used_test"
13
+
14
+ Bundler.setup
15
+ require 'active_record/railtie'
16
+ require 'is_this_used'
17
+ require 'rspec/rails'
18
+
19
+ require File.expand_path('spec/dummy_app/config/environment', __dir__)
20
+ ::IsThisUsedSpecMigrator.new.migrate
21
+ end
22
+
23
+ desc "Default: run all available test suites"
24
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- is_this_used (0.1.5)
4
+ is_this_used (0.1.7)
5
5
  activerecord (>= 5.2)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- is_this_used (0.1.5)
4
+ is_this_used (0.1.7)
5
5
  activerecord (>= 5.2)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- is_this_used (0.1.5)
4
+ is_this_used (0.1.7)
5
5
  activerecord (>= 5.2)
6
6
 
7
7
  GEM
@@ -6,6 +6,7 @@ class CreatePotentialCrufts < ActiveRecord::Migration<%= migration_version %>
6
6
  t.string :method_name, null: false
7
7
  t.string :method_type, null: false
8
8
  t.integer :invocations, null: false, default: 0
9
+ t.datetime :deleted_at
9
10
  t.timestamps
10
11
 
11
12
  t.index :owner_name
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IsThisUsed
4
+ class CruftCleaner
5
+ def self.clean🧹
6
+ IsThisUsed::PotentialCruft.where(deleted_at: nil).each do |potential_cruft|
7
+ unless potential_cruft.still_exists? && potential_cruft.still_tracked?
8
+ potential_cruft.update(deleted_at: Time.current)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -87,6 +87,8 @@ module IsThisUsed
87
87
  method_type: method_type
88
88
  )
89
89
 
90
+ IsThisUsed::Registry.instance << potential_cruft
91
+
90
92
  target_method.owner.define_method target_method.name do |*args|
91
93
  IsThisUsed::Util::LogSuppressor.suppress_logging do
92
94
  CruftTracker::Recorder.record_invocation(potential_cruft)
@@ -6,5 +6,39 @@ module IsThisUsed
6
6
  dependent: :destroy, class_name: 'IsThisUsed::PotentialCruftStack'
7
7
  has_many :potential_cruft_arguments,
8
8
  dependent: :destroy, class_name: 'IsThisUsed::PotentialCruftArgument'
9
+
10
+ def still_exists?
11
+ class_still_exists? && method_still_exists?
12
+ end
13
+
14
+ def still_tracked?
15
+ IsThisUsed::Registry.instance.include?(self)
16
+ end
17
+
18
+ def ==(other)
19
+ other.owner_name == owner_name &&
20
+ other.method_name == method_name &&
21
+ other.method_type == method_type
22
+ end
23
+
24
+ private
25
+
26
+ def class_still_exists?
27
+ Object.const_defined?(owner_name)
28
+ end
29
+
30
+ def clazz
31
+ owner_name.constantize
32
+ end
33
+
34
+ def method_still_exists?
35
+ case method_type
36
+ when IsThisUsed::CruftTracker::INSTANCE_METHOD
37
+ (clazz.instance_methods + clazz.private_instance_methods)
38
+ when IsThisUsed::CruftTracker::CLASS_METHOD
39
+ (clazz.methods + clazz.private_methods)
40
+ end.
41
+ include?(method_name.to_sym)
42
+ end
9
43
  end
10
44
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IsThisUsed
4
+ class Railtie < Rails::Railtie
5
+ initializer "is_this_used_railtie.configure_post_initialize_cleanup" do
6
+
7
+ config.after_initialize do
8
+ IsThisUsed::CruftCleaner.clean🧹
9
+ end
10
+
11
+ rescue StandardError
12
+ # Swallow all errors to prevent initialization failures.
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IsThisUsed
4
+ class Registry
5
+ include Singleton
6
+
7
+ attr_accessor :potential_crufts
8
+
9
+ def initialize
10
+ @potential_crufts = []
11
+ end
12
+
13
+ def <<(potential_cruft)
14
+ potential_crufts << potential_cruft
15
+ end
16
+
17
+ def include?(potential_cruft)
18
+ potential_crufts.include?(potential_cruft)
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IsThisUsed
4
- VERSION = '0.1.7'
4
+ VERSION = '0.1.8'
5
5
  end
data/lib/is_this_used.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'is_this_used/version'
2
2
  require 'is_this_used/cruft_tracker'
3
+ require 'is_this_used/cruft_cleaner'
4
+ require 'is_this_used/registry'
3
5
  require 'is_this_used/models/potential_cruft'
4
6
  require 'is_this_used/models/potential_cruft_stack'
5
7
  require 'is_this_used/models/potential_cruft_argument'
@@ -8,3 +10,6 @@ require 'is_this_used/util/log_suppressor'
8
10
  module IsThisUsed
9
11
  class Error < StandardError; end
10
12
  end
13
+
14
+ require 'is_this_used/railtie' if defined?(Rails)
15
+
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.7
4
+ version: 0.1.8
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-03-07 00:00:00.000000000 Z
11
+ date: 2022-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -238,10 +238,13 @@ files:
238
238
  - lib/generators/is_this_used/templates/create_potential_cruft_stacks.rb.erb
239
239
  - lib/generators/is_this_used/templates/create_potential_crufts.rb.erb
240
240
  - lib/is_this_used.rb
241
+ - lib/is_this_used/cruft_cleaner.rb
241
242
  - lib/is_this_used/cruft_tracker.rb
242
243
  - lib/is_this_used/models/potential_cruft.rb
243
244
  - lib/is_this_used/models/potential_cruft_argument.rb
244
245
  - lib/is_this_used/models/potential_cruft_stack.rb
246
+ - lib/is_this_used/railtie.rb
247
+ - lib/is_this_used/registry.rb
245
248
  - lib/is_this_used/util/log_suppressor.rb
246
249
  - lib/is_this_used/version.rb
247
250
  - log/test.log