Make deprecate work for non-exists methods

Before #33325, `deprecate` works for non-exist methods.
This is necessary, for example, if want to deprecate dynamically defined
methods like attributes methods.

Fixes #34646
This commit is contained in:
yuuji.yaginuma 2018-12-07 16:03:18 +09:00
parent f173ec77fc
commit 59ff1ba30d
2 changed files with 31 additions and 14 deletions

@ -52,27 +52,37 @@ def deprecate_methods(target_module, *method_names)
options = method_names.extract_options! options = method_names.extract_options!
deprecator = options.delete(:deprecator) || self deprecator = options.delete(:deprecator) || self
method_names += options.keys method_names += options.keys
mod = Module.new
method_names.each do |method_name| method_names.each do |method_name|
aliased_method, punctuation = method_name.to_s.sub(/([?!=])$/, ""), $1 if target_module.method_defined?(method_name) || target_module.private_method_defined?(method_name)
with_method = "#{aliased_method}_with_deprecation#{punctuation}" aliased_method, punctuation = method_name.to_s.sub(/([?!=])$/, ""), $1
without_method = "#{aliased_method}_without_deprecation#{punctuation}" with_method = "#{aliased_method}_with_deprecation#{punctuation}"
without_method = "#{aliased_method}_without_deprecation#{punctuation}"
target_module.send(:define_method, with_method) do |*args, &block| target_module.send(:define_method, with_method) do |*args, &block|
deprecator.deprecation_warning(method_name, options[method_name]) deprecator.deprecation_warning(method_name, options[method_name])
send(without_method, *args, &block) send(without_method, *args, &block)
end end
target_module.send(:alias_method, without_method, method_name) target_module.send(:alias_method, without_method, method_name)
target_module.send(:alias_method, method_name, with_method) target_module.send(:alias_method, method_name, with_method)
case case
when target_module.protected_method_defined?(without_method) when target_module.protected_method_defined?(without_method)
target_module.send(:protected, method_name) target_module.send(:protected, method_name)
when target_module.private_method_defined?(without_method) when target_module.private_method_defined?(without_method)
target_module.send(:private, method_name) target_module.send(:private, method_name)
end
else
mod.send(:define_method, method_name) do |*args, &block|
deprecator.deprecation_warning(method_name, options[method_name])
super(*args, &block)
end
end end
end end
target_module.prepend(mod) unless mod.instance_methods(false).empty?
end end
end end
end end

@ -31,6 +31,9 @@ def e; end
def f=(v); end def f=(v); end
deprecate :f= deprecate :f=
deprecate :g
def g ;end
module B module B
C = 1 C = 1
end end
@ -425,6 +428,10 @@ def test_custom_gem_name
end end
end end
def test_deprecate_work_before_define_method
assert_deprecated { @dtc.g }
end
private private
def deprecator_with_messages def deprecator_with_messages
klass = Class.new(ActiveSupport::Deprecation) klass = Class.new(ActiveSupport::Deprecation)