Class: Zold::AtomicFile
- Inherits:
-
Object
- Object
- Zold::AtomicFile
- Defined in:
- lib/zold/atomic_file.rb
Overview
Atomic file
Instance Method Summary collapse
-
#initialize(file) ⇒ AtomicFile
constructor
A new instance of AtomicFile.
- #read ⇒ Object
- #write(content) ⇒ Object
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
#read ⇒ Object
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 |