Sha256: 718668e4fe30c932a90b8bba009fbcb1cc7e5604e67d9e9de86d4d6f8ca8b73f
Contents?: true
Size: 1.03 KB
Versions: 1
Compression:
Stored size: 1.03 KB
Contents
require "file_writer/version" require "fileutils" # # Overwrite files safely # class FileWriter # The filename of the file to overwrite attr_reader :fname # Create new FileWriter # # @param String fname The name of he file to overwrite def initialize(fname) @fname = fname end # Overwrite the file # # @param String contents The new contents for the file. # def write(contents) backup = fname + "~" mode = File.stat(fname).mode begin File.rename(fname, backup) rescue SystemCallError FileUtils.cp(fname,fname+"~") end File.open(fname, "wb+", mode) do |f| if f.syswrite(contents) != contents.length raise SystemCallError.new("FileWriter#write: syswrite returned unexpected length") end f.flush f.fsync end end # Overwrite the file # # @param String fname The name of the file to overwrite # @param String contents The new contents for the file. # def self.write(fname, contents) FileWriter.new(fname).write(contents) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
file_writer-0.1.0 | lib/file_writer.rb |