module TrickBag::Io::TempFiles

Public Instance Methods

file_containing(text, file_prefix = '') { |filespec| ... } click to toggle source

For the easy creation and deletion of a temp file populated with text, wrapped around the code block you provide.

@param text the text to write to the temporary file @param file_prefix optional prefix for the temporary file's name @yield filespec of the temporary file

# File lib/trick_bag/io/temp_files.rb, line 15
def file_containing(text, file_prefix = '')
  raise "This method must be called with a code block." unless block_given?

  filespec = nil
  begin
    Tempfile.open(file_prefix) do |file|
      file << text
      filespec = file.path
    end
    yield(filespec)
  ensure
    File.delete(filespec) if filespec && File.exist?(filespec)
  end
end