Class: IOStreams::File::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/io_streams/file/writer.rb

Class Method Summary collapse

Class Method Details

.open(file_name, **args, &block) ⇒ Object

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.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/io_streams/file/writer.rb', line 9

def self.open(file_name, **args, &block)
  raise(ArgumentError, 'File name must be a string') unless file_name.is_a?(String)

  IOStreams::File::Path.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