lib/io_streams/file/writer.rb in iostreams-0.17.3 vs lib/io_streams/file/writer.rb in iostreams-0.18.0
- old
+ new
@@ -1,12 +1,22 @@
module IOStreams
module File
class Writer
# Write to a named file
+ #
+ # Note:
+ # If an exception is raised whilst the file is being written to the file is removed to
+ # prevent incomplete / partial files from being created.
def self.open(file_name, **args, &block)
raise(ArgumentError, 'File name must be a string') unless file_name.is_a?(String)
- ::File.open(file_name, 'wb', &block)
+ IOStreams.mkpath(file_name)
+ begin
+ ::File.open(file_name, 'wb', &block)
+ rescue StandardError => e
+ File.unlink(file_name) if File.exist?(file_name)
+ raise(e)
+ end
end
end
end
end