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

Instance Method Summary collapse

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 { |message, callstack|
   $stderr.puts(message)
   $stderr.puts callstack.join("\n  ") if klass.debug
 },
:log => Proc.new { |message, callstack|
   logger =
     if defined?(Rails) && Rails.logger
       Rails.logger
     else
       require 'active_support/logger'
       ActiveSupport::Logger.new($stderr)
     end
   logger.warn message
   logger.debug callstack.join("\n  ") if klass.debug
 },
 :notify => Proc.new { |message, callstack|
    ActiveSupport::Notifications.instrument("deprecation.#{klass.to_s}",
    :message => message, :callstack => callstack)
 },
 :raise => Proc.new { |message, callstack| raise message },
 :silence => Proc.new { |message, callstack| },
 :test => Proc.new { |message, callstack| self.deprecations << message }
  }
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 |message, callstack|
    deprecations << message
  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)
  options = method_names.extract_options!
  method_names += options.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},
              #{options[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, options = nil)

  options ||= {}

  if options.is_a? String  or options.is_a? Symbol
    message = options
    options = {}
  end

  warning = "#{method_name} is deprecated and will be removed from #{options[:deprecation_horizon] || context.deprecation_horizon}"
  case message
    when Symbol then "#{warning} (use #{message} instead)"
    when String then "#{warning} (#{message})"
    else warning
  end
end

.deprecationsObject



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, message = nil, callstack = caller)
  return if context.silenced
  deprecation_message(callstack, message).tap do |m|
    context.deprecation_behavior.each { |b| b.call(m, callstack) }
  end
end

Instance Method Details

#debugObject



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_behaviorObject

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 notify deprecation.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 { |message, 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_horizonObject



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

#silencedObject



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