Sha256: 236fa660fc7ad128299f468b548d53912ce18962c7c2024f095514d592cc93da

Contents?: true

Size: 620 Bytes

Versions: 31

Compression:

Stored size: 620 Bytes

Contents

# frozen_string_literal: true

module Uffizzi
  module FileHelper
    class << self
      def write_with_lock(path, data)
        lock(path) { atomic_write(path, "#{path}.tmp", data) }
      end

      def atomic_write(path, temp_path, content)
        File.open(temp_path, 'w') { |f| f.write(content) }
        FileUtils.mv(temp_path, path)
      end

      def lock(path)
        dir = File.dirname(path)
        FileUtils.mkdir_p(dir) unless File.directory?(dir)

        File.open(path).flock(File::LOCK_EX) if File.exist?(path)
        yield
        File.open(path).flock(File::LOCK_UN)
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
uffizzi-cli-2.4.11 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.10 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.9 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.8 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.7 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.6 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.5 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.4 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.3 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.2 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.1 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.4.0 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.3.4 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.3.3 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.3.2 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.3.1 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.3.0 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.2.2 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.2.1 lib/uffizzi/helpers/file_helper.rb
uffizzi-cli-2.2.0 lib/uffizzi/helpers/file_helper.rb