deprecate_soft 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f38db5179c81ba2e97d0a09bebeea87648eed96271b87a0089c3e444d122c9f5
4
+ data.tar.gz: fba061e831a26a0cf0be6bd99970cf84dd4130d3d93b6e53fbe01f5fa0b2846d
5
+ SHA512:
6
+ metadata.gz: 5d5d5ed40760cb78b41399924db249e93c585eef04f2f5d370c2cbfcfcfd5ae1621cea064c1b681e3ab90a16f63b814afe48c50ed5c4f4d32144602e7288ee49
7
+ data.tar.gz: 0124ae4b6a825c3a5920b2bee96417e73a44b9c19f527797cc35e7c852e43b5867369b04c0167a2efa2728eed8b7e99c894e9d74464ca09b36a8aef3c98bce64
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5 # purposely an old Ruby version
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: single_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: single_quotes
9
+
10
+ Metrics/AbcSize:
11
+ Enabled: false
12
+
13
+ Metrics/BlockLength:
14
+ Enabled: false
15
+
16
+ Metrics/PerceivedComplexity:
17
+ Enabled: false
18
+
19
+ Metrics/CyclomaticComplexity:
20
+ Enabled: false
21
+
22
+ Metrics/MethodLength:
23
+ Enabled: false
24
+
25
+ Lint/UnusedBlockArgument:
26
+ Enabled: false
27
+
28
+ Naming/MethodParameterName:
29
+ Enabled: false
30
+
31
+ Style/SafeNavigation:
32
+ Enabled: false
33
+
34
+ Style/Documentation:
35
+ Enabled: false
36
+
37
+ Style/RedundantSelf:
38
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## DepracateSoft Change Log
2
+
3
+ ## [1.0.0] - 2025-03-24
4
+ - Initial release
5
+ ## [0.0.1] - 2025-03-22
6
+ - pre-release
7
+
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Tilo Sloboda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,308 @@
1
+
2
+ # DeprecateSoft
3
+
4
+ DeprecateSoft is a lightweight and flexible Ruby gem designed to help you gracefully and safely deprecate methods.
5
+
6
+ It was inspired by the need to track deprecated method usage in large codebases before safely removing old code — with zero disruption and flexible metrics support.
7
+
8
+ This gem lets you wrap existing methods with before and after hooks to track usage patterns without changing the method's behavior, and without any impact on production systems.
9
+
10
+ It’s ideal for monitoring deprecated method usage across your application using non-blocking, low-latency tools such as Redis, DataDog, Prometheus, or logs.
11
+
12
+ Once tracking confirms that a deprecated method is no longer in use, you can confidently delete it from your codebase.
13
+
14
+ ---
15
+
16
+ ## ✨ Features
17
+
18
+ - Lightweight method wrapper for deprecation tracking
19
+ - Works with instance methods in any class or module
20
+ - Works with class or module methods in any class or module
21
+ - System-wide hook configuration (before and after)
22
+ - No monkey-patching or global pollution
23
+ - Fully compatible with Rails or plain Ruby apps
24
+
25
+ ---
26
+
27
+ ## 🚀 Installation
28
+
29
+ Add this to your Gemfile:
30
+
31
+ ```ruby
32
+ gem 'deprecate_soft'
33
+ ```
34
+
35
+ Then run:
36
+
37
+ ```sh
38
+ bundle install
39
+ ```
40
+
41
+ ---
42
+
43
+ ## ⚙️ Configuration
44
+
45
+ Create an initializer in your Rails app (or load manually in a non-Rails project):
46
+
47
+ ```ruby
48
+ # frozen_string_literal: true
49
+
50
+ require "deprecate_soft"
51
+
52
+ # Optional: set up your tracking solution
53
+
54
+ # require 'redis'
55
+ # redis = Redis.new(url: ENV.fetch("REDIS_URL", "redis://localhost:6379/0"))
56
+
57
+ # require 'datadog/statsd'
58
+ # statsd = Datadog::Statsd.new
59
+
60
+ DeprecateSoft.configure do |config|
61
+ # Required: define a before_hook to track method usage
62
+ #
63
+ # You can use Redis, StatsD, DataDog, Prometheus, etc.
64
+ config.before_hook = lambda do |method, message, args:|
65
+ # Track via Redis:
66
+ # redis_key = "deprecate_soft:#{method.gsub('#', ':')}"
67
+ # redis.incr("#{redis_key}")
68
+ # or:
69
+ # redis.incr("#{redis_key}:#{Date.today.cweek}") # weekly count
70
+
71
+ # Track via DataDog (StatsD):
72
+ # metric_name = "deprecate_soft.#{method.tr('#', '.').downcase}"
73
+ # statsd.increment(metric_name)
74
+
75
+ # Or just log it:
76
+ # Rails.logger.warn "DEPRECATED: #{method} – #{message}"
77
+ end
78
+
79
+ # Optional: define an after_hook for additional metrics/logging
80
+ #
81
+ # config.after_hook = lambda do |method, message, result:|
82
+ # # Optional: Logging or more metrics
83
+ # puts "[DD] #{method} completed after deprecated call"
84
+ # end
85
+
86
+ # Optional: customize how the original method is renamed internally
87
+ #
88
+ # For example, if you deprecate `foo`, this affects what the internal
89
+ # renamed method will be called. These names should be unlikely to conflict.
90
+ #
91
+ # Default is "__" and "deprecated", which becomes: "__foo_deprecated"
92
+ # config.prefix = "__"
93
+ # config.suffix = "deprecated"
94
+ end
95
+ ```
96
+
97
+ This setup ensures you can plug in **any tracking backend** you like — without polluting the global namespace.
98
+
99
+ ### 🔧 Customizing Method Name Wrapping
100
+
101
+ When `deprecate_soft` wraps a method, it renames the original method internally to preserve its behavior. You can customize how that internal method is named by configuring a `prefix` and `suffix`.
102
+
103
+ By default, the original method:
104
+
105
+ ```ruby
106
+ def foo
107
+ end
108
+
109
+ deprecate_soft :foo, "Use #bar instead"
110
+ ```
111
+
112
+ ...will be renamed to:
113
+
114
+ ```ruby
115
+ __foo_deprecated
116
+ ```
117
+
118
+ You can change the naming convention:
119
+
120
+ ```ruby
121
+ DeprecateSoft.configure do |config|
122
+ config.prefix = "legacy_" # or "" to disable
123
+ config.suffix = "old" # or nil to disable
124
+ end
125
+ ```
126
+
127
+ This gives you full control over how deprecated methods are renamed internally.
128
+
129
+ #### 📝 Naming Examples
130
+
131
+ | Prefix | Suffix | Method Name | Hidden Method Name |
132
+ |-------------|--------------|---------------|----------------------------|
133
+ | `"__"` | `"deprecated"` | `foo` | `__foo_deprecated` |
134
+ | `""` | `"old"` | `foo` | `foo_old` |
135
+ | `"legacy_"` | `""` | `foo` | `legacy_foo` |
136
+ | `"_"` | `"__"` | `foo` | `_foo__` |
137
+
138
+ These names are never called directly — they're used internally to wrap and preserve the original method logic.
139
+
140
+
141
+ ---
142
+
143
+ ## 🧩 Usage
144
+
145
+ 🚨 Always declare `deprecate_soft` **after** the method definition!
146
+
147
+ ### For Instance Methods:
148
+
149
+ ```ruby
150
+ class MyService
151
+ include DeprecateSoft
152
+
153
+ def deprecated_method(a, b)
154
+ puts "doing something with #{a} and #{b}"
155
+ end
156
+
157
+ deprecate_soft :deprecated_method, "Use #new_method instead"
158
+ end
159
+
160
+ MyService.new.deprecated_method(1, 2) # will exercise the tracking hooks
161
+ ```
162
+
163
+ ### For Class Methods:
164
+
165
+ ```ruby
166
+ class MyService
167
+ extend DeprecateSoft
168
+
169
+ def self.deprecated_method(a, b)
170
+ puts "doing something with #{a} and #{b}"
171
+ end
172
+
173
+ deprecate_soft :deprecated_method, "will be removed"
174
+ end
175
+
176
+ MyService.deprecated_method(1, 2) # will exercise the tracking hooks
177
+
178
+ ```
179
+
180
+ ---
181
+
182
+ ## 🔐 What It Does Under the Hood
183
+
184
+ When you call `deprecate_soft :method_name, "reason"`:
185
+
186
+ 1. It renames the original method to `__method_name_deprecated`.
187
+ 2. It defines a new method with the original name that:
188
+ - Calls the configured `before_hook` (if set)
189
+ - Delegates to the original method
190
+ - Calls the configured `after_hook` (if set)
191
+ 3. The optional `message` with the reason can help identifying alternatives.
192
+
193
+ This ensures consistent tracking, clean method resolution, and avoids accidental bypassing.
194
+
195
+ ---
196
+
197
+ ## 🧪 Example Hook Logic
198
+
199
+ You can track usage using whatever backend you like. Here are some quick examples:
200
+
201
+ ### Redis:
202
+
203
+ ```ruby
204
+ config.before_hook = lambda do |method, message, args:|
205
+ redis_key = "deprecate_soft:#{method.gsub('#', ':')}"
206
+ redis.incr("#{redis_key}:#{Date.today}")
207
+ end
208
+ ```
209
+
210
+ ### DataDog:
211
+
212
+ ```ruby
213
+ config.before_hook = lambda do |method, message, args:|
214
+ metric_name = "deprecate_soft.#{method.tr('#', '.').downcase}"
215
+ statsd.increment(metric_name)
216
+ end
217
+ ```
218
+
219
+ ### Log only:
220
+
221
+ ```ruby
222
+ config.before_hook = ->(method, message, args:) {
223
+ Rails.logger.warn "DEPRECATED: #{method} - #{message}"
224
+ }
225
+ ```
226
+
227
+ ## 🧪🧪🧪 Advaned Hook with Caller Tracking:
228
+
229
+ You can also track callers, so you can identify where you still have to fix your source code:
230
+
231
+
232
+ ### Redis:
233
+
234
+ ```ruby
235
+ # config/initializers/deprecate_soft.rb
236
+
237
+ require "deprecate_soft"
238
+ require 'redis'
239
+
240
+ # Configure your redis client instance
241
+ redis = Redis.new(url: ENV.fetch("REDIS_URL", "redis://localhost:6379/0"))
242
+
243
+ DeprecateSoft.configure do |config|
244
+ config.before_hook = lambda do |method, message, args:|
245
+ # Get the direct caller of the deprecated method
246
+ caller_info = caller_locations(1, 1).first
247
+
248
+ caller_key = if caller_info
249
+ "#{caller_info.path}:#{caller_info.lineno}"
250
+ else
251
+ "unknown"
252
+ end
253
+
254
+ # Track a global usage counter
255
+ redis.incr("deprecate_soft:#{method}")
256
+
257
+ # Track usage by caller location
258
+ redis.incr("deprecate_soft:#{method}:caller:#{caller_key}")
259
+
260
+ # Log the usage by caller location
261
+ Rails.logger.info("Deprecated #{method} called from #{caller_key}")
262
+ end
263
+ end
264
+ ```
265
+
266
+ If `Klass#legacy_method` is called from `app/services/foo.rb:42`, and `app/jobs/cleanup_job.rb:88`, you get:
267
+
268
+ ```
269
+ Klass#legacy_method → 7
270
+ Klass#legacy_method:caller:app/services/foo.rb:42 → 3
271
+ Klass#legacy_method:caller:app/jobs/cleanup_job.rb:88 → 4
272
+ ```
273
+
274
+ 💡 Now you not only know that the method is still used -- you know where from, and how often -- so you can fix your code.
275
+
276
+ ---
277
+
278
+ ## 🛡 Best Practices
279
+
280
+ - Use `deprecate_soft` for methods you plan to remove but want to confirm they are no longer used.
281
+ - Integrate with your observability platform for tracking.
282
+ - Review usage stats before deleting deprecated methods from your code.
283
+ - 🚨 Always declare `deprecate_soft` **after** the method definition! 🚨
284
+
285
+ ---
286
+
287
+ ## 🧰 Limitations / Notes
288
+
289
+ - Make sure hooks do not raise or interfere with production behavior.
290
+ - Only use non-blocking, low-latency methods for tracking!
291
+ - Currently assumes Ruby 2.5+ (for `&.` and keyword args support).
292
+ - Currently keeps the visibility of the renamed original method the same (does not make it private).
293
+
294
+ ---
295
+
296
+ ## 📬 Contributing
297
+
298
+ Feel free to open issues or pull requests if you'd like to:
299
+
300
+ - Add support for class methods
301
+ - Add Railtie for automatic setup
302
+ - Add built-in backends (e.g. Redis, StatsD)
303
+
304
+ ---
305
+
306
+ ## 📜 License
307
+
308
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'deprecate_soft'
4
+
5
+ # Optional: set up your tracking solution
6
+
7
+ # require 'redis'
8
+ # redis = Redis.new(url: ENV.fetch("REDIS_URL", "redis://localhost:6379/0"))
9
+
10
+ # require 'datadog/statsd'
11
+ # statsd = Datadog::Statsd.new
12
+
13
+ DeprecateSoft.configure do |config|
14
+ # Required: define a before_hook to track method usage
15
+ #
16
+ # You can use Redis, StatsD, DataDog, Prometheus, etc.
17
+ config.before_hook = lambda do |method, message, args:|
18
+ # Track via Redis:
19
+ # redis_key = "deprecate_soft:#{method.gsub('#', ':')}"
20
+ # redis.incr("#{redis_key}")
21
+ # or:
22
+ # redis.incr("#{redis_key}:#{Date.today.cweek}") # weekly count
23
+
24
+ # Track via DataDog (StatsD):
25
+ # metric_name = "deprecate_soft.#{method.tr('#', '.').downcase}"
26
+ # statsd.increment(metric_name)
27
+
28
+ # Or just log it:
29
+ # Rails.logger.warn "DEPRECATED: #{method} – #{message}"
30
+ end
31
+
32
+ # Optional: define an after_hook for additional metrics/logging
33
+ #
34
+ # config.after_hook = lambda do |method, message, result:|
35
+ # # Optional: Logging or more metrics
36
+ # puts "[DD] #{method} completed after deprecated call"
37
+ # end
38
+
39
+ # Optional: customize how the original method is renamed internally
40
+ #
41
+ # For example, if you deprecate `foo`, this affects what the internal
42
+ # renamed method will be called. These names should be unlikely to conflict.
43
+ #
44
+ # Default is "__" and "deprecated", which becomes: "__foo_deprecated"
45
+ # config.prefix = "__"
46
+ # config.suffix = "deprecated"
47
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeprecateSoft
4
+ module GlobalMonkeyPatch
5
+ def deprecate_soft(method_name, message)
6
+ if self.singleton_class.method_defined?(method_name) || self.singleton_class.private_method_defined?(method_name)
7
+ # Class method
8
+ DeprecateSoft::MethodWrapper.wrap_method(self, method_name, message, is_class_method: true)
9
+ elsif self.instance_methods.include?(method_name) || self.private_instance_methods.include?(method_name)
10
+ # Instance method
11
+ DeprecateSoft::MethodWrapper.wrap_method(self, method_name, message, is_class_method: false)
12
+ else # rubocop:disable Style/EmptyElse
13
+ # protect against declaring deprecate_soft before method is defined
14
+ #
15
+ # Do nothing — fail-safe in production
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeprecateSoft
4
+ module MethodWrapper
5
+ def self.wrap_method(context, method_name, message, is_class_method:)
6
+ hidden_method_name = "#{DeprecateSoft.prefix}#{method_name}_#{DeprecateSoft.suffix}"
7
+
8
+ if is_class_method
9
+ original_method = context.method(method_name)
10
+
11
+ context.singleton_class.define_method(hidden_method_name, original_method)
12
+
13
+ context.singleton_class.define_method(method_name) do |*args, &block|
14
+ full_name = "#{self.name}.#{method_name}"
15
+ DeprecateSoft.before_hook&.call(full_name, message, args: args) if defined?(DeprecateSoft.before_hook)
16
+ result = send(hidden_method_name, *args, &block)
17
+ DeprecateSoft.after_hook&.call(full_name, message, result: result) if defined?(DeprecateSoft.after_hook)
18
+ result
19
+ end
20
+ else
21
+ original_method = context.instance_method(method_name)
22
+
23
+ context.define_method(hidden_method_name, original_method)
24
+
25
+ context.define_method(method_name) do |*args, &block|
26
+ full_name = "#{self.class}##{method_name}"
27
+ DeprecateSoft.before_hook&.call(full_name, message, args: args) if defined?(DeprecateSoft.before_hook)
28
+ result = send(hidden_method_name, *args, &block)
29
+ DeprecateSoft.after_hook&.call(full_name, message, result: result) if defined?(DeprecateSoft.after_hook)
30
+ result
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeprecateSoft
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'deprecate_soft/version'
4
+
5
+ require_relative 'deprecate_soft/method_wrapper'
6
+
7
+ module DeprecateSoft
8
+ class << self
9
+ attr_accessor :before_hook, :after_hook
10
+ attr_writer :prefix, :suffix
11
+
12
+ def prefix
13
+ @prefix || '__'
14
+ end
15
+
16
+ def suffix
17
+ @suffix || 'deprecated'
18
+ end
19
+
20
+ def configure
21
+ yield self
22
+ end
23
+
24
+ def included(base)
25
+ base.extend InstanceMethods
26
+ end
27
+
28
+ def extended(base)
29
+ base.extend ClassMethods
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+ def deprecate_soft(method_name, message)
35
+ # protect against declaring deprecate_soft before method is defined
36
+ return unless method_defined?(method_name) || private_method_defined?(method_name)
37
+
38
+ DeprecateSoft::MethodWrapper.wrap_method(self, method_name, message, is_class_method: false)
39
+ end
40
+ end
41
+
42
+ module ClassMethods
43
+ def deprecate_soft(method_name, message)
44
+ # protect against declaring deprecate_soft before method is defined
45
+ return unless singleton_class.method_defined?(method_name) || singleton_class.private_method_defined?(method_name)
46
+
47
+ DeprecateSoft::MethodWrapper.wrap_method(self, method_name, message, is_class_method: true)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ module DeprecateSoft
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deprecate_soft
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tilo Sloboda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dogstatsd-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.21'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.21'
83
+ description: |
84
+ DeprecateSoft is a lightweight Ruby gem that lets you gracefully deprecate methods
85
+ in your codebase without breaking functionality. It wraps existing instance or
86
+ class methods and lets you plug in custom before/after hooks for tracking usage
87
+ via logging, Redis, DataDog, or any other observability tools.
88
+
89
+ Once you verify in your tracking that a method is no longer called,
90
+ you can remove it safely from your code base.
91
+
92
+ This is especially useful in large codebases where you want to safely remove
93
+ legacy methods, but first need insight into whether and where they're still
94
+ being called.
95
+
96
+ Hooks are configured once globally and apply project-wide. Fully compatible
97
+ with Rails or plain Ruby applications.
98
+ email:
99
+ - tilo.sloboda@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".rspec"
105
+ - ".rubocop.yml"
106
+ - CHANGELOG.md
107
+ - CODE_OF_CONDUCT.md
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - config/initializers/deprecate_soft.rb
112
+ - lib/deprecate_soft.rb
113
+ - lib/deprecate_soft/global_monkey_patch.rb
114
+ - lib/deprecate_soft/method_wrapper.rb
115
+ - lib/deprecate_soft/version.rb
116
+ - sig/deprecate_soft.rbs
117
+ homepage: https://github.com/tilo/deprecate_soft
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ homepage_uri: https://github.com/tilo/deprecate_soft
122
+ source_code_uri: https://github.com/tilo/deprecate_soft
123
+ changelog_uri: https://github.com/tilo/deprecate_soft/blob/main/CHANGELOG.md
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '2.5'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.5.4
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Gracefully deprecate and safely delete Ruby methods from your code
143
+ test_files: []