Class: Zold::AtomicFile

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/atomic_file.rb

Overview

Atomic file

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ AtomicFile

Returns a new instance of AtomicFile.


28
29
30
31
# File 'lib/zold/atomic_file.rb', line 28

def initialize(file)
  raise 'File can\'t be nil' if file.nil?
  @file = file
end

Instance Method Details

#readObject


33
34
35
36
37
38
# File 'lib/zold/atomic_file.rb', line 33

def read
  File.open(@file, 'rb') do |f|
    f.flock(File::LOCK_EX)
    f.read
  end
end

#write(content) ⇒ Object


40
41
42
43
44
45
46
# File 'lib/zold/atomic_file.rb', line 40

def write(content)
  raise 'Content can\'t be nil' if content.nil?
  File.open(@file, 'wb') do |f|
    f.flock(File::LOCK_EX)
    f.write(content)
  end
end