module Atomos

Constants

VERSION

Public Class Methods

default_tmpdir_for_file(dest, tmpdir) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/atomos.rb, line 33
def self.default_tmpdir_for_file(dest, tmpdir)
  tmpdir ||= begin
    require 'tmpdir'
    Dir.tmpdir
  end

  # Ensure the destination is on the same device as tmpdir
  if File.stat(tmpdir).dev != File.stat(File.dirname(dest)).dev
    # If not, use the directory of the destination as the tmpdir.
    tmpdir = File.dirname(dest)
  end

  tmpdir
end

Public Instance Methods

atomic_write(dest, contents = nil, tmpdir: nil) { |tmpfile| ... } click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/atomos.rb, line 9
def atomic_write(dest, contents = nil, tmpdir: nil, &block)
  unless contents.nil? ^ block.nil?
    raise ArgumentError, 'must provide either contents or a block'
  end

  tmpdir = Atomos.default_tmpdir_for_file(dest, tmpdir)

  require 'tempfile'
  Tempfile.open(".atomos.#{File.basename(dest)}", tmpdir) do |tmpfile|
    if contents
      tmpfile << contents
    else
      retval = yield tmpfile
    end

    tmpfile.close

    File.rename(tmpfile.path, dest)

    retval
  end
end