Class: Droonga::SafeFileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/safe_file_writer.rb

Class Method Summary collapse

Class Method Details

.write(path, contents = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/droonga/safe_file_writer.rb', line 23

def write(path, contents=nil)
  # Don't output the file directly to prevent loading of incomplete file!
  path = Pathname(path).expand_path
  FileUtils.mkdir_p(path.dirname.to_s)
  Tempfile.open(path.basename.to_s, path.dirname.to_s, "w") do |output|
    if block_given?
      yield(output, output.path)
    else
      output.write(contents)
    end
    output.flush
    File.rename(output.path, path.to_s)
  end
end