Sha256: 5a438efe5a6e4bf6d83f293ffb80abe2c0e3f57f0f22e5c0e522ab2ed1fe717f

Contents?: true

Size: 567 Bytes

Versions: 13

Compression:

Stored size: 567 Bytes

Contents

module Spruz
  module Write
    # Write to a file atomically
    def write(filename, content = nil, mode = 'w')
      temp = File.new(filename + ".tmp.#$$.#{Time.now.to_f}", mode)
      if content.nil? and block_given?
        yield temp
      elsif !content.nil?
        temp.write content
      else
        raise ArgumentError, "either content or block argument required"
      end
      temp.close
      File.rename temp.path, filename
    ensure
      temp and !temp.closed? and temp.close
      File.file?(temp.path) and File.unlink temp.path
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
spruz-0.2.11 lib/spruz/write.rb
spruz-0.2.10 lib/spruz/write.rb
spruz-0.2.9 lib/spruz/write.rb
spruz-0.2.8 lib/spruz/write.rb
spruz-0.2.7 lib/spruz/write.rb
spruz-0.2.6 lib/spruz/write.rb
spruz-0.2.5 lib/spruz/write.rb
spruz-0.2.2 lib/spruz/write.rb
spruz-0.2.1 lib/spruz/write.rb
spruz-0.2.0 lib/spruz/write.rb
spruz-0.1.5 lib/spruz/write.rb
spruz-0.1.3 lib/spruz/write.rb
spruz-0.1.2 lib/spruz/write.rb