Module: TrickBag::Io::TempFiles

Defined in:
lib/trick_bag/io/temp_files.rb

Class Method Summary collapse

Class Method Details

.file_containing(text, file_prefix = '') { ... } ⇒ Object

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

Parameters:

  • text

    the text to write to the temporary file

  • file_prefix (defaults to: '')

    optional prefix for the temporary file’s name

Yields:

  • filespec of the temporary file



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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