Class: DevSuite::Utils::FileWriter::AtomicWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/dev_suite/utils/file_writer/atomic_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, content, mode: 0o644) ⇒ AtomicWriter

Returns a new instance of AtomicWriter.



7
8
9
10
11
# File 'lib/dev_suite/utils/file_writer/atomic_writer.rb', line 7

def initialize(path, content, mode: 0o644)
  @path = path
  @content = content
  @mode = mode
end

Instance Method Details

#writeObject



13
14
15
16
17
18
19
20
# File 'lib/dev_suite/utils/file_writer/atomic_writer.rb', line 13

def write
  ensure_directory_exists

  ::File.open(@path, "w") do |file|
    file.flock(::File::LOCK_EX) # Lock for exclusive write access
    write_to_tempfile_and_replace
  end
end