mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
71993c6f97
ActiveSupport::Deprecation is now a class rather than a module. You can get instance of ActiveSupport::Deprecation calling #instance method. ActiveSupport::Deprecation.instance But when you need to get new object od ActiveSupport::Deprecation you need to just call #new. @instance = ActiveSupport::Deprecation.new Since you can create a new object, you can change the version and the name of the library where the deprecator concerned. ActiveSupport::Deprecation.new('2.0', 'MyGem') If you need use another deprecator instance you can select it in the options of deprecate method. deprecate :method, :deprecator => deprecator_instance Documentation has been updated.
44 lines
1.6 KiB
Ruby
44 lines
1.6 KiB
Ruby
require 'active_support/core_ext/module/aliasing'
|
|
require 'active_support/core_ext/array/extract_options'
|
|
|
|
module ActiveSupport
|
|
class Deprecation
|
|
module MethodWrapper
|
|
# Declare that a method has been deprecated.
|
|
#
|
|
# module Fred
|
|
# extend self
|
|
#
|
|
# def foo; end
|
|
# def bar; end
|
|
# def baz; end
|
|
# end
|
|
#
|
|
# ActiveSupport::Deprecation.deprecate_methods(Fred, :foo, bar: :qux, baz: 'use Bar#baz instead')
|
|
# # => [:foo, :bar, :baz]
|
|
#
|
|
# Fred.foo
|
|
# # => "DEPRECATION WARNING: foo is deprecated and will be removed from Rails 4.1."
|
|
#
|
|
# Fred.bar
|
|
# # => "DEPRECATION WARNING: bar is deprecated and will be removed from Rails 4.1 (use qux instead)."
|
|
#
|
|
# Fred.baz
|
|
# # => "DEPRECATION WARNING: baz is deprecated and will be removed from Rails 4.1 (use Bar#baz instead)."
|
|
def deprecate_methods(target_module, *method_names)
|
|
options = method_names.extract_options!
|
|
deprecator = options.delete(:deprecator) || ActiveSupport::Deprecation.instance
|
|
method_names += options.keys
|
|
|
|
method_names.each do |method_name|
|
|
target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation|
|
|
target_module.send(:define_method, "#{target}_with_deprecation#{punctuation}") do |*args, &block|
|
|
deprecator.deprecation_warning(method_name, options[method_name], caller)
|
|
send(:"#{target}_without_deprecation#{punctuation}", *args, &block)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|