diff options
author | Piotr Niełacny <piotr.nielacny@gmail.com> | 2012-09-13 08:38:34 +0200 |
---|---|---|
committer | Piotr Niełacny <piotr.nielacny@gmail.com> | 2012-09-13 08:42:00 +0200 |
commit | 71993c6f9770b1350aa41fe8c68f1dd2c7800403 (patch) | |
tree | 515425716f332400ee5df8fb6515cc0c26ce6a0a /activesupport/lib/active_support/deprecation/reporting.rb | |
parent | 2c690a0f5b36896da9b003d4e24159a27ebd7f71 (diff) | |
download | rails-71993c6f9770b1350aa41fe8c68f1dd2c7800403.tar.gz rails-71993c6f9770b1350aa41fe8c68f1dd2c7800403.tar.bz2 rails-71993c6f9770b1350aa41fe8c68f1dd2c7800403.zip |
Change ActiveSupport::Deprecation to class.
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.
Diffstat (limited to 'activesupport/lib/active_support/deprecation/reporting.rb')
-rw-r--r-- | activesupport/lib/active_support/deprecation/reporting.rb | 135 |
1 files changed, 76 insertions, 59 deletions
diff --git a/activesupport/lib/active_support/deprecation/reporting.rb b/activesupport/lib/active_support/deprecation/reporting.rb index 69de79b93c..cf91ca1acb 100644 --- a/activesupport/lib/active_support/deprecation/reporting.rb +++ b/activesupport/lib/active_support/deprecation/reporting.rb @@ -1,73 +1,90 @@ module ActiveSupport - module Deprecation - attr_accessor :silenced + class Deprecation + module Reporting + # Whether to print a message (silent mode) + attr_accessor :silenced + # Name of gem where method is deprecated + attr_accessor :gem_name - # Outputs a deprecation warning to the output configured by - # <tt>ActiveSupport::Deprecation.behavior</tt>. - # - # ActiveSupport::Deprecation.warn("something broke!") - # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)" - def warn(message = nil, callstack = caller) - return if silenced - deprecation_message(callstack, message).tap do |m| - behavior.each { |b| b.call(m, callstack) } + # Outputs a deprecation warning to the output configured by + # <tt>ActiveSupport::Deprecation.behavior</tt>. + # + # ActiveSupport::Deprecation.warn("something broke!") + # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)" + def warn(message = nil, callstack = caller) + return if silenced + deprecation_message(callstack, message).tap do |m| + behavior.each { |b| b.call(m, callstack) } + end end - end - # Silence deprecation warnings within the block. - # - # ActiveSupport::Deprecation.warn("something broke!") - # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)" - # - # ActiveSupport::Deprecation.silence do - # ActiveSupport::Deprecation.warn("something broke!") - # end - # # => nil - def silence - old_silenced, @silenced = @silenced, true - yield - ensure - @silenced = old_silenced - end + # Silence deprecation warnings within the block. + # + # ActiveSupport::Deprecation.warn("something broke!") + # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)" + # + # ActiveSupport::Deprecation.silence do + # ActiveSupport::Deprecation.warn("something broke!") + # end + # # => nil + def silence + old_silenced, @silenced = @silenced, true + yield + ensure + @silenced = old_silenced + end - def deprecated_method_warning(method_name, message = nil) - warning = "#{method_name} is deprecated and will be removed from Rails #{deprecation_horizon}" - case message - when Symbol then warning << " (use #{message} instead)" - when String then warning << " (#{message})" + def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = caller) + deprecated_method_warning(deprecated_method_name, message).tap do |message| + warn(message, caller_backtrace) + end end - warning - end - private - - def deprecation_message(callstack, message = nil) - message ||= "You are using deprecated behavior which will be removed from the next major or minor release." - message += '.' unless message =~ /\.$/ - "DEPRECATION WARNING: #{message} #{deprecation_caller_message(callstack)}" - end + private + # Outputs a deprecation warning message + # ActiveSupport::Deprecation.deprecated_method_warning(:method_name) + # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon}" + # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, :another_method) + # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (use another_method instead)" + # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, "Optional message") + # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (Optional message)" + def deprecated_method_warning(method_name, message = nil) + warning = "#{method_name} is deprecated and will be removed from #{gem_name} #{deprecation_horizon}" + case message + when Symbol then "#{warning} (use #{message} instead)" + when String then "#{warning} (#{message})" + else warning + end + end - def deprecation_caller_message(callstack) - file, line, method = extract_callstack(callstack) - if file - if line && method - "(called from #{method} at #{file}:#{line})" - else - "(called from #{file}:#{line})" + def deprecation_message(callstack, message = nil) + message ||= "You are using deprecated behavior which will be removed from the next major or minor release." + message += '.' unless message =~ /\.$/ + "DEPRECATION WARNING: #{message} #{deprecation_caller_message(callstack)}" end - end - end - def extract_callstack(callstack) - rails_gem_root = File.expand_path("../../../../..", __FILE__) + "/" - offending_line = callstack.find { |line| !line.start_with?(rails_gem_root) } || callstack.first - if offending_line - if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/) - md.captures - else - offending_line + def deprecation_caller_message(callstack) + file, line, method = extract_callstack(callstack) + if file + if line && method + "(called from #{method} at #{file}:#{line})" + else + "(called from #{file}:#{line})" + end + end + end + + def extract_callstack(callstack) + rails_gem_root = File.expand_path("../../../../..", __FILE__) + "/" + offending_line = callstack.find { |line| !line.start_with?(rails_gem_root) } || callstack.first + if offending_line + if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/) + md.captures + else + offending_line + end + end end - end end end end |