From 96cc93a75b419a4da4320a752c7c18e916a37346 Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sun, 15 Jan 2012 23:45:08 -0800 Subject: [PATCH] bug fixes for edit-method --- lib/pry.rb | 4 ++-- lib/pry/default_commands/introspection.rb | 13 ++++++++++++- lib/pry/helpers/command_helpers.rb | 6 +++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index 0606b49f..91f79b92 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -146,8 +146,8 @@ class Pry # CommandErrors are caught by the REPL loop and displayed to the user. They # indicate an exceptional condition that's fatal to the current command. - class CommandError < StandardError - end + class CommandError < StandardError; end + class NonMethodContextError < CommandError; end end require "method_source" diff --git a/lib/pry/default_commands/introspection.rb b/lib/pry/default_commands/introspection.rb index 1daeff98..f3c8994b 100644 --- a/lib/pry/default_commands/introspection.rb +++ b/lib/pry/default_commands/introspection.rb @@ -249,11 +249,22 @@ class Pry raise CommandError, "No editor set!\nEnsure that #{text.bold("Pry.config.editor")} is set to your editor of choice." end - @method = method_object rescue nil + begin + @method = method_object + rescue NonMethodContextError => err + end if opts.present?(:patch) || (@method && @method.dynamically_defined?) + if err + raise err # can't patch a non-method + end + process_patch else + if err && !File.exist?(target.eval('__FILE__')) + raise err # can't edit a non-file + end + process_file end end diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 3aa9ff13..afe5f8ed 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -29,7 +29,7 @@ class Pry if name && !meth command_error("The method '#{name}' could not be found.", omit_help) elsif !meth - command_error("No method name given, and context is not a method.", omit_help) + command_error("No method name given, and context is not a method.", omit_help, NonMethodContextError) end (opts[:super] || 0).times do @@ -44,9 +44,9 @@ class Pry meth end - def command_error(message, omit_help) + def command_error(message, omit_help, klass=CommandError) message += " Type `#{command_name} --help` for help." unless omit_help - raise CommandError, message + raise klass, message end def make_header(meth, content=meth.source)