2013-01-05 14:31:20 -05:00
|
|
|
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
|
2013-01-05 14:37:37 -05:00
|
|
|
output.puts "#{obj_name} was reloaded!"
|
2013-01-05 14:31:20 -05:00
|
|
|
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)
|
2013-01-05 14:37:37 -05:00
|
|
|
raise CommandError, "Cannot reload #{obj_name} as it has no associated file on disk. File found was: #{code_object.source_file}"
|
2013-01-05 14:31:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Pry::Commands.add_command(Pry::Command::ReloadCode)
|
|
|
|
end
|