aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/deprecation/method_wrappers.rb
blob: f0c3e37e65d1dcded33eda1505218ccf51759f54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

require "active_support/core_ext/array/extract_options"

module ActiveSupport
  class Deprecation
    module MethodWrapper
      # Declare that a method has been deprecated.
      #
      #   class Fred
      #     def aaa; end
      #     def bbb; end
      #     def ccc; end
      #     def ddd; end
      #     def eee; end
      #   end
      #
      # Using the default deprecator:
      #   ActiveSupport::Deprecation.deprecate_methods(Fred, :aaa, bbb: :zzz, ccc: 'use Bar#ccc instead')
      #   # => Fred
      #
      #   Fred.new.aaa
      #   # DEPRECATION WARNING: aaa is deprecated and will be removed from Rails 5.1. (called from irb_binding at (irb):10)
      #   # => nil
      #
      #   Fred.new.bbb
      #   # DEPRECATION WARNING: bbb is deprecated and will be removed from Rails 5.1 (use zzz instead). (called from irb_binding at (irb):11)
      #   # => nil
      #
      #   Fred.new.ccc
      #   # DEPRECATION WARNING: ccc is deprecated and will be removed from Rails 5.1 (use Bar#ccc instead). (called from irb_binding at (irb):12)
      #   # => 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]
      #
      #   Fred.new.ddd
      #   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]
      #
      #   Fred.new.eee
      #   DEPRECATION WARNING: eee is deprecated and will be removed from MyGem next-release (use zzz instead). (called from irb_binding at (irb):18)
      #   # => nil
      def deprecate_methods(target_module, *method_names)
        options = method_names.extract_options!
        deprecator = options.delete(:deprecator) || self
        method_names += options.keys
        mod = Module.new

        method_names.each do |method_name|
          if target_module.method_defined?(method_name) || target_module.private_method_defined?(method_name)
            aliased_method, punctuation = method_name.to_s.sub(/([?!=])$/, ""), $1
            with_method = "#{aliased_method}_with_deprecation#{punctuation}"
            without_method = "#{aliased_method}_without_deprecation#{punctuation}"

            target_module.send(:define_method, with_method) do |*args, &block|
              deprecator.deprecation_warning(method_name, options[method_name])
              send(without_method, *args, &block)
            end

            target_module.send(:alias_method, without_method, method_name)
            target_module.send(:alias_method, method_name, with_method)

            case
            when target_module.protected_method_defined?(without_method)
              target_module.send(:protected, method_name)
            when target_module.private_method_defined?(without_method)
              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

        target_module.prepend(mod) unless mod.instance_methods(false).empty?
      end
    end
  end
end