1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Minor refactor of reload-code command

The main things here are:
* DRYing up the process method a little bit
* Replacing "obj.class == Class" with "Class ===", because the latter is
  more reliable in the face of BasicObject and classes that define
  `#class`
This commit is contained in:
Ryan Fitzgerald 2014-04-27 16:37:29 -07:00
parent 94c2b4c420
commit 249dbabe59

View file

@ -15,20 +15,12 @@ class Pry
BANNER
def process
if obj_name.empty?
# if no parameters were provided then try to reload the
# current file (i.e target.eval("__FILE__"))
if _pry_.current_context.eval("self").class == Class
@obj_name = "self"
code_object = Pry::CodeObject.lookup("self", _pry_)
reload_code_object(code_object)
else
reload_current_file
end
if !args.empty?
reload_object(args.join(" "))
elsif Class === target.eval("self")
reload_object("self")
else
code_object = Pry::CodeObject.lookup(obj_name, _pry_)
reload_code_object(code_object)
reload_current_file
end
end
@ -47,21 +39,20 @@ class Pry
output.puts "The current file: #{current_file} was reloaded!"
end
def reload_code_object(code_object)
check_for_reloadability(code_object)
def reload_object(identifier)
code_object = Pry::CodeObject.lookup(identifier, _pry_)
check_for_reloadability(code_object, identifier)
load code_object.source_file
output.puts "#{obj_name} was reloaded!"
output.puts "#{identifier} was reloaded!"
end
def obj_name
@obj_name ||= args.join(" ")
end
def check_for_reloadability(code_object)
def check_for_reloadability(code_object, identifier)
if !code_object || !code_object.source_file
raise CommandError, "Cannot locate #{obj_name}!"
raise CommandError, "Cannot locate #{identifier}!"
elsif !File.exists?(code_object.source_file)
raise CommandError, "Cannot reload #{obj_name} as it has no associated file on disk. File found was: #{code_object.source_file}"
raise CommandError,
"Cannot reload #{identifier} as it has no associated file on disk. " \
"File found was: #{code_object.source_file}"
end
end
end