~brightbox/brightbox/deb-rubygems

« back to all changes in this revision

Viewing changes to lib/rubygems/deprecate.rb

  • Committer: John Leach
  • Date: 2012-01-31 21:06:26 UTC
  • mfrom: (16.1.26)
  • Revision ID: git-v1:f7ca52cea23c74c94576d0f199f91e04b34a594c
Merge remote-tracking branch 'debian/master'

Conflicts:
        debian/changelog

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#         # ...
12
12
#       end
13
13
#
14
 
#       extend Deprecate
 
14
#       extend Gem::Deprecate
15
15
#       deprecate :instance_method, "X.z", 2011, 4
16
16
#
17
17
#       class << self
18
 
#         extend Deprecate
 
18
#         extend Gem::Deprecate
19
19
#         deprecate :klass_method, :none, 2011, 4
20
20
#       end
21
21
#     end
22
22
 
23
 
module Deprecate
24
 
 
25
 
  def self.skip # :nodoc:
26
 
    @skip ||= false
27
 
  end
28
 
 
29
 
  def self.skip= v # :nodoc:
30
 
    @skip = v
31
 
  end
32
 
 
33
 
  ##
34
 
  # Temporarily turn off warnings. Intended for tests only.
35
 
 
36
 
  def skip_during
37
 
    Deprecate.skip, original = true, Deprecate.skip
38
 
    yield
39
 
  ensure
40
 
    Deprecate.skip = original
41
 
  end
42
 
 
43
 
  ##
44
 
  # Simple deprecation method that deprecates +name+ by wrapping it up
45
 
  # in a dummy method. It warns on each call to the dummy method
46
 
  # telling the user of +repl+ (unless +repl+ is :none) and the
47
 
  # year/month that it is planned to go away.
48
 
 
49
 
  def deprecate name, repl, year, month
50
 
    class_eval {
51
 
      old = "_deprecated_#{name}"
52
 
      alias_method old, name
53
 
      define_method name do |*args, &block| # TODO: really works on 1.8.7?
54
 
        klass = self.kind_of? Module
55
 
        target = klass ? "#{self}." : "#{self.class}#"
56
 
        msg = [ "NOTE: #{target}#{name} is deprecated",
57
 
                repl == :none ? " with no replacement" : ", use #{repl}",
58
 
                ". It will be removed on or after %4d-%02d-01." % [year, month],
59
 
                "\n#{target}#{name} called from #{Gem.location_of_caller.join(":")}",
60
 
              ]
61
 
        warn "#{msg.join}." unless Deprecate.skip
62
 
        send old, *args, &block
63
 
      end
64
 
    }
65
 
  end
66
 
 
67
 
  module_function :deprecate, :skip_during
 
23
module Gem
 
24
  module Deprecate
 
25
 
 
26
    def self.skip # :nodoc:
 
27
      @skip ||= false
 
28
    end
 
29
 
 
30
    def self.skip= v # :nodoc:
 
31
      @skip = v
 
32
    end
 
33
 
 
34
    ##
 
35
    # Temporarily turn off warnings. Intended for tests only.
 
36
 
 
37
    def skip_during
 
38
      Gem::Deprecate.skip, original = true, Gem::Deprecate.skip
 
39
      yield
 
40
    ensure
 
41
      Gem::Deprecate.skip = original
 
42
    end
 
43
 
 
44
    ##
 
45
    # Simple deprecation method that deprecates +name+ by wrapping it up
 
46
    # in a dummy method. It warns on each call to the dummy method
 
47
    # telling the user of +repl+ (unless +repl+ is :none) and the
 
48
    # year/month that it is planned to go away.
 
49
 
 
50
    def deprecate name, repl, year, month
 
51
      class_eval {
 
52
        old = "_deprecated_#{name}"
 
53
        alias_method old, name
 
54
        define_method name do |*args, &block| # TODO: really works on 1.8.7?
 
55
          klass = self.kind_of? Module
 
56
          target = klass ? "#{self}." : "#{self.class}#"
 
57
          msg = [ "NOTE: #{target}#{name} is deprecated",
 
58
            repl == :none ? " with no replacement" : ", use #{repl}",
 
59
            ". It will be removed on or after %4d-%02d-01." % [year, month],
 
60
            "\n#{target}#{name} called from #{Gem.location_of_caller.join(":")}",
 
61
          ]
 
62
          warn "#{msg.join}." unless Gem::Deprecate.skip
 
63
          send old, *args, &block
 
64
        end
 
65
      }
 
66
    end
 
67
 
 
68
    module_function :deprecate, :skip_during
 
69
  end
68
70
end