2013-01-05 14:31:20 -05:00
|
|
|
class Pry
|
|
|
|
class Command::ReloadCode < Pry::ClassCommand
|
|
|
|
match 'reload-code'
|
|
|
|
group 'Misc'
|
2013-01-09 15:23:19 -05:00
|
|
|
description 'Reload the source file that contains the specified code object.'
|
|
|
|
|
|
|
|
banner <<-'BANNER'
|
|
|
|
Reload the source file that contains the specified code object.
|
|
|
|
BANNER
|
2013-01-05 14:31:20 -05:00
|
|
|
|
|
|
|
def process
|
2013-01-11 14:34:48 -05:00
|
|
|
code_object = Pry::CodeObject.lookup(obj_name, _pry_)
|
2013-01-05 14:31:20 -05:00
|
|
|
|
|
|
|
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
|
2013-01-05 15:17:07 -05:00
|
|
|
@obj_name ||= args.empty? ? "self" : args.join(" ")
|
2013-01-05 14:31:20 -05:00
|
|
|
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)
|
2013-01-28 16:51:05 -05:00
|
|
|
Pry::Commands.alias_command 'reload-method', 'reload-code'
|
2013-01-05 14:31:20 -05:00
|
|
|
end
|