Class: Fluent::GithubActivities::SafeFileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/github-activities/safe_file_writer.rb

Class Method Summary collapse

Class Method Details

.write(path, contents = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fluent/plugin/github-activities/safe_file_writer.rb', line 28

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) 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