2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/core_ext/array/extract_options"
|
2019-04-16 03:43:08 -04:00
|
|
|
require "active_support/core_ext/module/redefine_method"
|
2009-04-18 00:29:30 -04:00
|
|
|
|
|
|
|
module ActiveSupport
|
2012-09-13 02:38:34 -04:00
|
|
|
class Deprecation
|
|
|
|
module MethodWrapper
|
|
|
|
# Declare that a method has been deprecated.
|
|
|
|
#
|
2017-11-27 13:51:24 -05:00
|
|
|
# class Fred
|
2015-10-13 14:10:14 -04:00
|
|
|
# def aaa; end
|
|
|
|
# def bbb; end
|
|
|
|
# def ccc; end
|
|
|
|
# def ddd; end
|
|
|
|
# def eee; end
|
2012-09-13 02:38:34 -04:00
|
|
|
# end
|
|
|
|
#
|
2015-10-13 14:10:14 -04:00
|
|
|
# Using the default deprecator:
|
|
|
|
# ActiveSupport::Deprecation.deprecate_methods(Fred, :aaa, bbb: :zzz, ccc: 'use Bar#ccc instead')
|
2017-02-13 19:24:05 -05:00
|
|
|
# # => Fred
|
2012-09-13 02:38:34 -04:00
|
|
|
#
|
2017-11-27 13:51:24 -05:00
|
|
|
# Fred.new.aaa
|
2016-01-14 02:10:38 -05:00
|
|
|
# # DEPRECATION WARNING: aaa is deprecated and will be removed from Rails 5.1. (called from irb_binding at (irb):10)
|
2015-10-13 14:10:14 -04:00
|
|
|
# # => nil
|
2012-09-13 02:38:34 -04:00
|
|
|
#
|
2017-11-27 13:51:24 -05:00
|
|
|
# Fred.new.bbb
|
2016-01-14 02:10:38 -05:00
|
|
|
# # DEPRECATION WARNING: bbb is deprecated and will be removed from Rails 5.1 (use zzz instead). (called from irb_binding at (irb):11)
|
2015-10-13 14:10:14 -04:00
|
|
|
# # => nil
|
2012-09-13 02:38:34 -04:00
|
|
|
#
|
2017-11-27 13:51:24 -05:00
|
|
|
# Fred.new.ccc
|
2016-01-14 02:10:38 -05:00
|
|
|
# # DEPRECATION WARNING: ccc is deprecated and will be removed from Rails 5.1 (use Bar#ccc instead). (called from irb_binding at (irb):12)
|
2015-10-13 14:10:14 -04:00
|
|
|
# # => nil
|
|
|
|
#
|
|
|
|
# Passing in a custom deprecator:
|
|
|
|
# custom_deprecator = ActiveSupport::Deprecation.new('next-release', 'MyGem')
|
|
|
|
# ActiveSupport::Deprecation.deprecate_methods(Fred, ddd: :zzz, deprecator: custom_deprecator)
|
|
|
|
# # => [:ddd]
|
|
|
|
#
|
2017-11-27 13:51:24 -05:00
|
|
|
# Fred.new.ddd
|
2015-10-13 14:10:14 -04:00
|
|
|
# DEPRECATION WARNING: ddd is deprecated and will be removed from MyGem next-release (use zzz instead). (called from irb_binding at (irb):15)
|
|
|
|
# # => nil
|
|
|
|
#
|
|
|
|
# Using a custom deprecator directly:
|
|
|
|
# custom_deprecator = ActiveSupport::Deprecation.new('next-release', 'MyGem')
|
|
|
|
# custom_deprecator.deprecate_methods(Fred, eee: :zzz)
|
|
|
|
# # => [:eee]
|
|
|
|
#
|
2017-11-27 13:51:24 -05:00
|
|
|
# Fred.new.eee
|
2015-10-13 14:10:14 -04:00
|
|
|
# DEPRECATION WARNING: eee is deprecated and will be removed from MyGem next-release (use zzz instead). (called from irb_binding at (irb):18)
|
|
|
|
# # => nil
|
2012-09-13 02:38:34 -04:00
|
|
|
def deprecate_methods(target_module, *method_names)
|
|
|
|
options = method_names.extract_options!
|
2015-10-13 14:10:14 -04:00
|
|
|
deprecator = options.delete(:deprecator) || self
|
2012-09-13 02:38:34 -04:00
|
|
|
method_names += options.keys
|
2019-04-16 03:43:08 -04:00
|
|
|
mod = nil
|
2009-04-18 00:29:30 -04:00
|
|
|
|
2018-07-10 12:03:54 -04:00
|
|
|
method_names.each do |method_name|
|
2018-12-07 02:03:18 -05:00
|
|
|
if target_module.method_defined?(method_name) || target_module.private_method_defined?(method_name)
|
2019-04-16 03:43:08 -04:00
|
|
|
method = target_module.instance_method(method_name)
|
|
|
|
target_module.redefine_method(method_name) do |*args, &block|
|
2018-12-07 02:03:18 -05:00
|
|
|
deprecator.deprecation_warning(method_name, options[method_name])
|
2019-04-16 03:43:08 -04:00
|
|
|
method.bind(self).call(*args, &block)
|
2018-12-07 02:03:18 -05:00
|
|
|
end
|
|
|
|
else
|
2019-04-16 03:43:08 -04:00
|
|
|
mod ||= Module.new
|
2018-12-20 11:39:18 -05:00
|
|
|
mod.define_method(method_name) do |*args, &block|
|
2018-12-07 02:03:18 -05:00
|
|
|
deprecator.deprecation_warning(method_name, options[method_name])
|
|
|
|
super(*args, &block)
|
|
|
|
end
|
2018-07-10 12:03:54 -04:00
|
|
|
end
|
|
|
|
end
|
2018-12-07 02:03:18 -05:00
|
|
|
|
2019-04-16 03:43:08 -04:00
|
|
|
target_module.prepend(mod) if mod
|
2009-04-18 00:29:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|