Sha256: 5132d6850aeb72e5ef7a0f3edd40b448ddebd95f9314ca6f7235f042a7607b39

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

require "securerandom" # for uuid generation
require "stud/with"
require "fileutils"

module Stud
  module Temporary
    include Stud::With

    # Returns a string for a randomly-generated temporary path.
    #
    # This does not create any files.
    def pathname(prefix="")
      root = ENV["TMP"] || ENV["TMPDIR"] || ENV["TEMP"] || "/tmp"
      return File.join(root, "#{prefix}-#{SecureRandom.uuid}")
    end

    # Return a File handle to a randomly-generated path.
    #
    # Any arguments beyond the first (prefix) argument will be
    # given to File.new.
    #
    # If no file args are given, the default file mode is "w+"
    def file(prefix="", *args, &block)
      args << "w+" if args.empty?
      if block_given?
        with(File.new(pathname(prefix), *args)) do |fd|
          begin
            block.call(fd)
            fd.close
          ensure
            File.unlink(fd.path)
          end
        end
      else
        return File.new(pathname(prefix), *args)
      end
    end

    # Make a temporary directory.
    #
    # If given a block, the directory path is given to the block.  WHen the
    # block finishes, the directory and all its contents will be deleted.
    #
    # If no block given, it will return the path to a newly created directory.
    # You are responsible for then cleaning up.
    def directory(prefix="", &block)
      path = pathname(prefix)
      Dir.mkdir(path)

      if block_given?
        begin
          block.call(path)
        ensure
          FileUtils.rm_r(path)
        end
      else
        return path
      end
    end
  end # module Temporary

  extend self
end # module Stud

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stud-0.0.15 lib/stud/temporary.rb