-
Notifications
You must be signed in to change notification settings - Fork 22k
Allow ActiveSupport::Deprecation features to be used by rails applications and library authors #6348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
carlosantoniodasilva
merged 2 commits into
rails:master
from
LTe:no_global_depreactations
Sep 13, 2012
Merged
Allow ActiveSupport::Deprecation features to be used by rails applications and library authors #6348
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 33 additions & 1 deletion
34
activesupport/lib/active_support/core_ext/module/deprecation.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,42 @@ | ||
require 'active_support/deprecation/method_wrappers' | ||
|
||
class Module | ||
# Declare that a method has been deprecated. | ||
# deprecate :foo | ||
# deprecate :bar => 'message' | ||
# deprecate :foo, :bar, :baz => 'warning!', :qux => 'gone!' | ||
# | ||
# You can use custom deprecator instance | ||
# deprecate :foo, :deprecator => MyLib::Deprecator.new | ||
# deprecate :foo, :bar => "warning!", :deprecator => MyLib::Deprecator.new | ||
# | ||
# \Custom deprecators must respond to one method | ||
# [deprecation_warning(deprecated_method_name, message, caller_backtrace)] will be called with the deprecated | ||
# method name, the message it was declared | ||
# with and caller_backtrace. Implement | ||
# whatever warning behavior you like here. | ||
# | ||
# Example | ||
# class MyLib::Deprecator | ||
# | ||
# def deprecation_warning(deprecated_method_name, message, caller_backtrace) | ||
# message = "#{method_name} is deprecated and will be removed from MyLibrary | #{message}" | ||
# Kernel.warn message | ||
# end | ||
# | ||
# end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove whitespaces between the method and class declaration, there's no need for them ✂️ |
||
# | ||
# module MyLib | ||
# mattr_accessor :deprecator | ||
# self.deprecator = Deprecator.new | ||
# end | ||
# | ||
# When we deprecate method | ||
# class MyLib::Bar | ||
# deprecate :foo => "this is very old method", :deprecator => MyLib.deprecator | ||
# end | ||
# | ||
# It will build deprecation message and invoke deprecator warning by calling | ||
# MyLib.deprecator.deprecation_warning(:foo, "this is a very old method", caller) | ||
def deprecate(*method_names) | ||
ActiveSupport::Deprecation.deprecate_methods(self, *method_names) | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,40 @@ | ||
require 'active_support/core_ext/module/deprecation' | ||
require 'active_support/deprecation/instance_delegator' | ||
require 'active_support/deprecation/behaviors' | ||
require 'active_support/deprecation/reporting' | ||
require 'active_support/deprecation/method_wrappers' | ||
require 'active_support/deprecation/proxy_wrappers' | ||
require 'singleton' | ||
|
||
module ActiveSupport | ||
module Deprecation | ||
class << self | ||
# The version the deprecated behavior will be removed, by default. | ||
attr_accessor :deprecation_horizon | ||
end | ||
self.deprecation_horizon = '4.1' | ||
# \Deprecation specifies the API used by Rails to deprecate | ||
# methods, instance variables, objects and constants. | ||
# The API depends on four methods: | ||
# | ||
# * +initialize+ which expects two parameters | ||
# described below; | ||
class Deprecation | ||
include Singleton | ||
include InstanceDelegator | ||
include Behavior | ||
include Reporting | ||
include MethodWrapper | ||
|
||
# The version the deprecated behavior will be removed, by default. | ||
attr_accessor :deprecation_horizon | ||
|
||
# By default, warnings are not silenced and debugging is off. | ||
self.silenced = false | ||
self.debug = false | ||
# It accepts two parameters on initialization. The first is an version of library | ||
# and the second is an library name | ||
# | ||
# == Example | ||
# | ||
# ActiveSupport::Deprecation.new('2.0', 'MyLibrary') | ||
def initialize(deprecation_horizon = '4.1', gem_name = 'Rails') | ||
self.gem_name = gem_name | ||
self.deprecation_horizon = deprecation_horizon | ||
# By default, warnings are not silenced and debugging is off. | ||
self.silenced = false | ||
self.debug = false | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
activesupport/lib/active_support/deprecation/instance_delegator.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'active_support/core_ext/kernel/singleton_class' | ||
require 'active_support/core_ext/module/delegation' | ||
|
||
module ActiveSupport | ||
class Deprecation | ||
module InstanceDelegator | ||
def self.included(base) | ||
base.extend(ClassMethods) | ||
base.public_class_method :new | ||
end | ||
|
||
module ClassMethods | ||
def include(included_module) | ||
included_module.instance_methods.each { |m| method_added(m) } | ||
super | ||
end | ||
|
||
def method_added(method_name) | ||
singleton_class.delegate(method_name, to: :instance) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use a custom deprecator object that responds to +warn+ and +deprecated_method_warning+. This is useful when you use +ActiveSupport::Deprecation+ in your own libraries and want to customize deprecation behavior.