Sha256: 31769645580e5ad97bbd80b96a04d6eff211274a74aa7014d39d81129f4b6da1

Contents?: true

Size: 678 Bytes

Versions: 77

Compression:

Stored size: 678 Bytes

Contents

require 'tempfile'

# Write to a file atomically.  Useful for situations where you don't
# want other processes or threads to see half-written files.
#
#  File.atomic_write("important.file") do |file|
#    file.write("hello")
#  end
#
# If your temp directory is not on the same filesystem as the file you're 
# trying to write, you can provide a different temporary directory.
# 
# File.atomic_write("/data/something.imporant", "/data/tmp") do |f|
#   file.write("hello")
# end
def File.atomic_write(file_name, temp_dir = Dir.tmpdir)
  temp_file = Tempfile.new(File.basename(file_name), temp_dir)
  yield temp_file
  temp_file.close
  File.rename(temp_file.path, file_name)
end

Version data entries

77 entries across 77 versions & 5 rubygems

Version Path
activesupport-2.0.0 lib/active_support/core_ext/file.rb
activesupport-2.0.1 lib/active_support/core_ext/file.rb
activesupport-2.0.2 lib/active_support/core_ext/file.rb
activesupport-2.0.4 lib/active_support/core_ext/file.rb
activesupport-2.0.5 lib/active_support/core_ext/file.rb
backlog-0.0.0 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.0.1 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.0.2 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.0.5 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.0.4 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.1.0 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.1.1 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.1.2 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.2.0 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.2.1 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.3.0 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.3.1 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.3.2 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.3.3 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.3.4 vendor/rails/activesupport/lib/active_support/core_ext/file.rb