Module: Uffizzi::FileHelper

Defined in:
lib/uffizzi/helpers/file_helper.rb

Class Method Summary collapse

Class Method Details

.atomic_write(path, temp_path, content) ⇒ Object



10
11
12
13
# File 'lib/uffizzi/helpers/file_helper.rb', line 10

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

.lock(path) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/uffizzi/helpers/file_helper.rb', line 15

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

.write_with_lock(path, data) ⇒ Object



6
7
8
# File 'lib/uffizzi/helpers/file_helper.rb', line 6

def write_with_lock(path, data)
  lock(path) { atomic_write(path, "#{path}.tmp", data) }
end