Created
March 13, 2019 14:18
-
-
Save leifcr/ef761cc831c9369bbfb500b8450fdd48 to your computer and use it in GitHub Desktop.
irbrc/pryrc debugging (see https://www.strongopinionsweaklytyped.com/blog/2013/03/17/simple-active-record-query-debugging-in-the-rails-console/)
This file contains 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
def add_debugger(clazz, method) | |
debugger_method = binding.respond_to?(:pry) ? 'binding.pry' : 'byebug' | |
unless clazz.method_defined? "#{method}_with_debugger" | |
clazz.class_eval <<-CODE, __FILE__, __LINE__ + 1 | |
def #{method}_with_debugger(*args, &block) | |
#{debugger_method} | |
#{method}_without_debugger(*args, &block) | |
end | |
alias_method_chain :#{method}, :debugger | |
CODE | |
end | |
end | |
def remove_debugger(clazz, method) | |
return unless clazz.method_defined? "#{method}_with_debugger" | |
clazz.class_eval do | |
alias_method method, "#{method}_without_debugger" | |
undef_method "#{method}_with_debugger" | |
undef_method "#{method}_without_debugger" | |
end | |
end | |
# add_debugger MyModel.singleton_class, :create | |
# remove_debugger MyModel.singleton_class, :create |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment