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 32 |
# File 'lib/zold/atomic_file.rb', line 28 def initialize(file) raise 'File can\'t be nil' if file.nil? @file = file @mutex = Mutex.new end |
Instance Method Details
#read ⇒ Object
34 35 36 37 38 |
# File 'lib/zold/atomic_file.rb', line 34 def read @mutex.synchronize do File.open(@file, 'rb', &:read) end end |
#write(content) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/zold/atomic_file.rb', line 40 def write(content) raise 'Content can\'t be nil' if content.nil? @mutex.synchronize do File.open(@file, 'wb') do |f| f.write(content) end end end |