The best way to deprecate all class methods in Rails

Viewed 452

I have a class (and it is not an ActiveRecord model) having multiple class methods. All the class methods must be deprecated. What's the best way to do this?

class MyClass
  class << self
    def method_to_deprecate_1
      ...
    end
    ...
    def method_to_deprecate_100
      ...
    end
  end
end
1 Answers

Ruby has a special module Gem::Deprecate for that, here's an example from the official documentation:

class Legacy
  def self.klass_method
    # ...
  end

  def instance_method
    # ...
  end

  extend Gem::Deprecate
  deprecate :instance_method, "X.z", 2011, 4

  class << self
    extend Gem::Deprecate
    deprecate :klass_method, :none, 2011, 4
  end
end

It will result to:

2.5.0 :020 > Legacy.new.instance_method
NOTE: Legacy#instance_method is deprecated; use X.z instead. It will be removed on or after 2011-04-01.
Legacy#instance_method called from (irb):20.
 => nil 
2.5.0 :021 > Legacy.klass_method
NOTE: Legacy.klass_method is deprecated with no replacement. It will be removed on or after 2011-04-01.
Legacy.klass_method called from (irb):21.
 => nil

Edit: To answer your question directly, here is the most elegant way I could imagine to deprecate ALL of the class methods:

class Kek
   class << self
     def old_method_1
       # ...
     end

     def old_method_2
       # ...
     end

     extend Gem::Deprecate

     # instance methods here are our actual class methods + all of the Object's methods from Ruby
     instance_methods(false).each { |method_to_deprecate| deprecate(method_to_deprecate, :none, 2011, 4) }
   end
end
Related