Permalink
Browse files

renamed reload-method to reload-code and extended functionality

Should now be able to reload any valid code object, including: methods, modules, commands, procs, etc
  • Loading branch information...
1 parent 80e612b commit 38e49b16d3098c7bfb997a8f32e59b1c537a93c9 @banister banister committed Jan 5, 2013
Showing with 35 additions and 23 deletions.
  1. +35 −0 lib/pry/commands/reload_code.rb
  2. +0 −23 lib/pry/commands/reload_method.rb
@@ -0,0 +1,35 @@
+class Pry
+ class Command::ReloadCode < Pry::ClassCommand
+ match 'reload-code'
+ group 'Misc'
+ description 'Reload the source file that contains the specified code object'
+
+ def process
+ code_object = Pry::CodeObject.lookup(obj_name, target, _pry_)
+
+ check_for_reloadability(code_object)
+ reload_code_object(code_object)
+ end
+
+ private
+
+ def reload_code_object(code_object)
+ load code_object.source_file
+ output.puts "#{code_object} was reloaded!"
+ end
+
+ def obj_name
+ @obj_name ||= args.empty? ? nil : args.join(" ")
+ end
+
+ def check_for_reloadability(code_object)
+ if !code_object
+ raise CommandError, "Cannot locate #{obj_name}!"
+ elsif !File.exists?(code_object.source_file)
+ raise CommandError, "Cannot reload #{code_object} as it has no associated file on disk. File found was: #{code_object.source_file}"
+ end
+ end
+ end
+
+ Pry::Commands.add_command(Pry::Command::ReloadCode)
+end
@@ -1,23 +0,0 @@
-class Pry
- class Command::ReloadMethod < Pry::ClassCommand
- match 'reload-method'
- group 'Misc'
- description 'Reload the source file that contains the specified method'
-
- def process(meth_name)
- meth = get_method_or_raise(meth_name, target, {}, :omit_help)
-
- if meth.source_type == :c
- raise CommandError, "Can't reload a C method."
- elsif meth.dynamically_defined?
- raise CommandError, "Can't reload an eval method."
- else
- file_name = meth.source_file
- load file_name
- output.puts "Reloaded #{file_name}."
- end
- end
- end
-
- Pry::Commands.add_command(Pry::Command::ReloadMethod)
-end

0 comments on commit 38e49b1

Please sign in to comment.