Important
I archived this repo here and moved it to Codeberg for several reasons.
🛠️ It's still active there!
This gem provides transparent declaration of deprecated methods and classes. It's easy, small, has no dependencies and no overhead.
- Gem: rubygems.org
- Source: codeberg.org
The simplest way to install Deprecations gem is to use Bundler.
Add Deprecations to your Gemfile:
gem 'deprecations'and install it by running Bundler:
$ bundle add deprecationsTo install the gem globally use:
$ gem install deprecationsAfter adding the gem to your project
require 'deprecations'you can specify which methods and classes are deprecated. To mark a method as deprecated is quite easy:
class MySample
def clear
# something here
end
def clean
clear
end
deprecated :clean, :clear, 'next version'
endWhenever the method MySample#clean is called this warning appears:
warning:
MySample#cleanis deprecated and will be outdated next version. Please useMySample#clearinstead.
Marking a complete class as deprecated will present the deprecation warning whenever this class is instantiated:
class MySample
deprecated!
# some more code here...
endYou can change the behavior of notifying:
Deprecations.behavior = :raiseThere are 3 pre-defined behaviors:
:raisewill raise anDeprecations::Exceptionwhen a deprecated method is called:silencewill do nothing (ignore the deprecation):warnwill print a warning (default behavior):deprecatedwill print a warning when Ruby's warning category 'deprecated' is enabled
Besides this you can implement your own:
Deprecations.behavior =
proc do |subject, _alternative, _outdated|
SuperLogger.warning "deprecated: #{subject}"
endAny object responding to #call will be accepted as a valid handler.
Whenever you need to temporary change the standard behavior (like e.g. in your specs) you can do this like
Deprecations.with_behavior(:silent) { MyDeprecatedClass.new.do_some_magic }Please have a look at the specs for detailed information and more samples.