Sha256: 3df403898d1effb0ff6c267f52d323f1666eb5dd7875fc377a5e6c5511471327

Contents?: true

Size: 957 Bytes

Versions: 1

Compression:

Stored size: 957 Bytes

Contents

require 'tempfile'

module Ftpd
  class TempDir

    attr_reader :path

    class << self

      def make(basename = nil)
        temp_dir = TempDir.new(basename)
        begin
          yield(temp_dir)
        ensure
          temp_dir.rm unless temp_dir.kept
        end
      end

    end

    attr_reader :kept

    def initialize(basename = nil)
      @path = unique_path(basename)
      @kept = false
      ObjectSpace.define_finalizer(self, TempDir.cleanup(path))
      Dir.mkdir(@path)
    end

    def keep
      @kept = true
      ObjectSpace.undefine_finalizer(self)
    end

    def rm
      keep
      system("rm -rf #{path.inspect}")
    end

    private

    def unique_path(basename)
      tempfile = Tempfile.new(File.basename(basename || $0 || ''))
      path = tempfile.path
      tempfile.close!
      path
    end

    def TempDir.cleanup(path)
      proc { |id|
        system("/bin/rm -rf #{path.inspect}")
      }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ftpd-0.0.0.pre2 lib/ftpd/temp_dir.rb