Skip to content

Instantly share code, notes, and snippets.

@leifcr
Created March 13, 2019 14:18
Show Gist options
  • Save leifcr/ef761cc831c9369bbfb500b8450fdd48 to your computer and use it in GitHub Desktop.
Save leifcr/ef761cc831c9369bbfb500b8450fdd48 to your computer and use it in GitHub Desktop.
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