Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Last active December 21, 2017 05:33
Show Gist options
  • Save a2ikm/202d46a2bcff2faab100914a2faf509b to your computer and use it in GitHub Desktop.
Save a2ikm/202d46a2bcff2faab100914a2faf509b to your computer and use it in GitHub Desktop.
Log deprecated methods in Rails app
DEPRECATE_LOGGER = Logger.new(Rails.root.join("log", "deprecate.log").to_s).tap do |logger|
logger.formatter = proc { |s, time, p, msg| "[#{time}] #{msg}\n" }
end
class Module
def __deprecate(meth)
aliased, punc = meth.to_s.sub(/([?!=])$/, ""), $1
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{aliased}_with_deprecated#{punc}(*args, &block)
DEPRECATE_LOGGER.debug { "\#{self.class.name}##{meth} was called from \#{caller(1, 1).first}." }
__send__(:#{aliased}_without_deprecated#{punc}, *args, &block)
end
alias_method_chain :#{meth}, :deprecated
RUBY
end
def __deprecate_accessor(meth)
__deprecate(meth)
__deprecate("#{meth}=")
end
def __deprecate_reader(meth)
__deprecate(meth)
end
def __deprecate_writer(meth)
__deprecate("#{meth}=")
end
end
__deprecate def old_method(a, b, &bkock)
:
end
attr_accessor :old_accessor
__deprecate_accessor :old_accessor
attr_reader :old_reader
__deprecate_reader :old_reader
attr_writer :old_writer
__deprecate_writer :old_writer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment