Last active
August 29, 2015 14:00
-
-
Save kballenegger/36816df3829a3d4974bc to your computer and use it in GitHub Desktop.
Overriding wrapping routes with a deprecation message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'kenji' | |
| module Kenji | |
| class Controller | |
| class << self | |
| def routes_deprecated!(enabled = true) | |
| if enabled | |
| puts "Enabling deprecation for the following methods in #{self.name}:" | |
| else | |
| puts "Disabling deprecation in #{self.name}." | |
| end | |
| @deprecation_enabled = enabled | |
| end | |
| old_route = instance_method(:route) | |
| define_method(:route) do |*args, &block| | |
| if @deprecation_enabled | |
| # block -> unbound method | |
| define_method(:_tmp_route_action, &block) | |
| block = instance_method(:_tmp_route_action) | |
| remove_method(:_tmp_route_action) | |
| wrapped = lambda do |*a| | |
| warn "[DEPRECATED] route #{args} was called with args: #{a}" | |
| block.bind(self).call(*a) | |
| end | |
| old_route.bind(self).call(*args, &wrapped) | |
| puts "- route #{args}" | |
| else | |
| old_route.bind(self).call(*args, &block) | |
| end | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment