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
backlog-0.6.3 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.6.5 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.6.4 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.0 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.6.6 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.1 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.10 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.12 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.11 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.2 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.3 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.5 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.4 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.7 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.6 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.8 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.7.9 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.8.0 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.9.0 vendor/rails/activesupport/lib/active_support/core_ext/file.rb
backlog-0.8.1 vendor/rails/activesupport/lib/active_support/core_ext/file.rb