Module: Deprecation
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/deprecation.rb,
lib/deprecation/version.rb,
lib/deprecation/behaviors.rb,
lib/deprecation/reporting.rb,
lib/deprecation/method_wrappers.rb
Constant Summary collapse
- VERSION =
"0.0.3"
Class Method Summary collapse
- .behaviors(klass) ⇒ Object
- .collect(context) ⇒ Object
-
.deprecate_methods(target_module, *method_names) ⇒ Object
Declare that a method has been deprecated.
- .deprecated_method_warning(context, method_name, options = nil) ⇒ Object
- .deprecations ⇒ Object
-
.silence(context) ⇒ Object
Silence deprecation warnings within the block.
-
.warn(context, message = nil, callstack = caller) ⇒ Object
Outputs a deprecation warning to the output configured by
ActiveSupport::Deprecation.behavior
.
Instance Method Summary collapse
- #debug ⇒ Object
- #debug=(bool) ⇒ Object
-
#deprecation_behavior ⇒ Object
Returns the current behavior or if one isn’t set, defaults to
:stderr
. -
#deprecation_behavior=(deprecation_behavior) ⇒ Object
Sets the behavior to the specified value.
- #deprecation_horizon ⇒ Object
- #deprecation_horizon=(horizon) ⇒ Object
- #silenced ⇒ Object
- #silenced=(bool) ⇒ Object
Class Method Details
.behaviors(klass) ⇒ Object
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 |
# File 'lib/deprecation/behaviors.rb', line 39 def self.behaviors klass # Default warning behaviors per Rails.env. { :stderr => Proc.new { |, callstack| $stderr.puts() $stderr.puts callstack.join("\n ") if klass.debug }, :log => Proc.new { |, callstack| logger = if defined?(Rails) && Rails.logger Rails.logger else require 'active_support/logger' ActiveSupport::Logger.new($stderr) end logger.warn logger.debug callstack.join("\n ") if klass.debug }, :notify => Proc.new { |, callstack| ActiveSupport::Notifications.instrument("deprecation.#{klass.to_s}", :message => , :callstack => callstack) }, :raise => Proc.new { |, callstack| raise }, :silence => Proc.new { |, callstack| }, :test => Proc.new { |, callstack| self.deprecations << } } end |
.collect(context) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/deprecation/reporting.rb', line 22 def collect(context) old_behavior = context.deprecation_behavior deprecations = [] context.deprecation_behavior = Proc.new do |, callstack| deprecations << end result = yield [result, deprecations] ensure context.deprecation_behavior = old_behavior end |
.deprecate_methods(target_module, *method_names) ⇒ Object
Declare that a method has been deprecated.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/deprecation/method_wrappers.rb', line 6 def self.deprecate_methods(target_module, *method_names) = method_names. method_names += .keys method_names.each do |method_name| target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation| target_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1) def #{target}_with_deprecation#{punctuation}(*args, &block) Deprecation.warn(#{target_module.to_s}, Deprecation.deprecated_method_warning(#{target_module.to_s}, :#{method_name}, #{[method_name].inspect}), caller ) send(:#{target}_without_deprecation#{punctuation}, *args, &block) end end_eval end end end |
.deprecated_method_warning(context, method_name, options = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/deprecation/reporting.rb', line 34 def deprecated_method_warning(context, method_name, = nil) ||= {} if .is_a? String or .is_a? Symbol = = {} end warning = "#{method_name} is deprecated and will be removed from #{[:deprecation_horizon] || context.deprecation_horizon}" case when Symbol then "#{warning} (use #{} instead)" when String then "#{warning} (#{})" else warning end end |
.deprecations ⇒ Object
35 36 37 |
# File 'lib/deprecation/behaviors.rb', line 35 def self.deprecations @deprecations ||= [] end |
.silence(context) ⇒ Object
Silence deprecation warnings within the block.
15 16 17 18 19 20 |
# File 'lib/deprecation/reporting.rb', line 15 def silence context old_silenced, context.silenced = context.silenced, true yield ensure context.silenced = old_silenced end |
.warn(context, message = nil, callstack = caller) ⇒ Object
Outputs a deprecation warning to the output configured by ActiveSupport::Deprecation.behavior
Deprecation.warn("something broke!")
# => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
7 8 9 10 11 12 |
# File 'lib/deprecation/reporting.rb', line 7 def warn(context, = nil, callstack = caller) return if context.silenced (callstack, ).tap do |m| context.deprecation_behavior.each { |b| b.call(m, callstack) } end end |
Instance Method Details
#debug ⇒ Object
26 27 28 |
# File 'lib/deprecation.rb', line 26 def debug @debug end |
#debug=(bool) ⇒ Object
30 31 32 |
# File 'lib/deprecation.rb', line 30 def debug= bool @debug = bool end |
#deprecation_behavior ⇒ Object
Returns the current behavior or if one isn’t set, defaults to :stderr
7 8 9 |
# File 'lib/deprecation/behaviors.rb', line 7 def deprecation_behavior @deprecation_behavior ||= [Deprecation.behaviors(self)[:stderr]] end |
#deprecation_behavior=(deprecation_behavior) ⇒ Object
Sets the behavior to the specified value. Can be a single value, array, or an object that responds to call
.
Available behaviors:
stderr
-
Log all deprecation warnings to $stderr.
log
-
Log all deprecation warnings to
Rails.logger
. - +notify
-
Use
ActiveSupport::Notifications
to notifydeprecation.rails
. silence
-
Do nothing.
Setting behaviors only affects deprecations that happen after boot time. Deprecation warnings raised by gems are not affected by this setting because they happen before Rails boots up.
Deprecation.deprecation_behavior = :stderr
Deprecation.deprecation_behavior = [:stderr, :log]
Deprecation.deprecation_behavior = MyCustomHandler
Deprecation.deprecation_behavior = proc { |, callstack|
# custom stuff
}
31 32 33 |
# File 'lib/deprecation/behaviors.rb', line 31 def deprecation_behavior=(deprecation_behavior) @deprecation_behavior = Array(deprecation_behavior).map { |b| Deprecation.behaviors(self)[b] || b } end |
#deprecation_horizon ⇒ Object
14 15 16 |
# File 'lib/deprecation.rb', line 14 def deprecation_horizon @deprecation_horizon end |
#deprecation_horizon=(horizon) ⇒ Object
10 11 12 |
# File 'lib/deprecation.rb', line 10 def deprecation_horizon= horizon @deprecation_horizon = horizon end |
#silenced ⇒ Object
18 19 20 |
# File 'lib/deprecation.rb', line 18 def silenced @silenced end |
#silenced=(bool) ⇒ Object
22 23 24 |
# File 'lib/deprecation.rb', line 22 def silenced= bool @silenced = bool end |