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