2010-06-29 15:18:17 -04:00
|
|
|
require "active_support/notifications"
|
2010-07-01 04:26:45 -04:00
|
|
|
require "active_support/core_ext/array/wrap"
|
2010-06-29 15:18:17 -04:00
|
|
|
|
2009-04-18 00:29:30 -04:00
|
|
|
module ActiveSupport
|
|
|
|
module Deprecation
|
|
|
|
class << self
|
|
|
|
# Whether to print a backtrace along with the warning.
|
|
|
|
attr_accessor :debug
|
|
|
|
|
2010-12-19 20:50:47 -05:00
|
|
|
# Returns the set behaviour or if one isn't set, defaults to +:stderr+
|
2009-04-18 00:29:30 -04:00
|
|
|
def behavior
|
2010-07-01 05:59:10 -04:00
|
|
|
@behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
|
2009-04-18 00:29:30 -04:00
|
|
|
end
|
|
|
|
|
2010-12-19 20:50:47 -05:00
|
|
|
# Sets the behaviour to the specified value. Can be a single value or an array.
|
|
|
|
#
|
|
|
|
# Examples
|
|
|
|
#
|
|
|
|
# ActiveSupport::Deprecation.behavior = :stderr
|
|
|
|
# ActiveSupport::Deprecation.behavior = [:stderr, :log]
|
2010-06-29 15:18:17 -04:00
|
|
|
def behavior=(behavior)
|
2010-07-01 04:26:45 -04:00
|
|
|
@behavior = Array.wrap(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
|
2009-04-18 00:29:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-29 15:18:17 -04:00
|
|
|
# Default warning behaviors per Rails.env.
|
2009-04-18 00:29:30 -04:00
|
|
|
DEFAULT_BEHAVIORS = {
|
2010-06-29 15:18:17 -04:00
|
|
|
:stderr => Proc.new { |message, callstack|
|
2009-04-18 00:29:30 -04:00
|
|
|
$stderr.puts(message)
|
|
|
|
$stderr.puts callstack.join("\n ") if debug
|
|
|
|
},
|
2010-06-29 15:18:17 -04:00
|
|
|
:log => Proc.new { |message, callstack|
|
2009-10-14 20:54:45 -04:00
|
|
|
logger =
|
|
|
|
if defined?(Rails) && Rails.logger
|
|
|
|
Rails.logger
|
|
|
|
else
|
|
|
|
require 'logger'
|
|
|
|
Logger.new($stderr)
|
|
|
|
end
|
2009-04-18 00:29:30 -04:00
|
|
|
logger.warn message
|
|
|
|
logger.debug callstack.join("\n ") if debug
|
2010-06-29 15:18:17 -04:00
|
|
|
},
|
|
|
|
:notify => Proc.new { |message, callstack|
|
|
|
|
ActiveSupport::Notifications.instrument("deprecation.rails",
|
|
|
|
:message => message, :callstack => callstack)
|
2009-04-18 00:29:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|