Class: CdoTempfileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/cdo.rb

Overview

Helper module for easy temp file handling {{{

Constant Summary collapse

N =

base for persitent temp files - just for debugging

10000000

Instance Method Summary collapse

Constructor Details

#initialize(dir = Dir.tmpdir) ⇒ CdoTempfileStore

Returns a new instance of CdoTempfileStore.


519
520
521
522
523
524
525
526
527
528
529
# File 'lib/cdo.rb', line 519

def initialize(dir=Dir.tmpdir)
  @dir                  = dir
  @tag                  = 'Cdorb'
  @persistent_tempfiles = false

  # storage for filenames in order to prevent too early removement
  @_tempfiles           = []

  # make sure the tempdir ie really there
  Dir.mkdir(@dir) unless Dir.exists?(@dir)
end

Instance Method Details

#cleanTempDirObject


552
553
554
555
556
557
# File 'lib/cdo.rb', line 552

def cleanTempDir
  # filter by name, realfile and ownership
  Dir.entries(@dir).map {|f| "#@dir/#{f}"}.find_all {|file|
    File.file?(file) and File.owned?(file) and file.include?(@tag)
  }.each {|f| File.unlink(f)}
end

#newFileObject


535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/cdo.rb', line 535

def newFile
  unless @persistent_tempfiles
    t = Tempfile.new(@tag,@dir)
    @_tempfiles << t
    @_tempfiles << t.path
    t.path
  else
    t = "_"+rand(N).to_s
    @_tempfiles << t
    t
  end
end

#setPersist(value) ⇒ Object


531
532
533
# File 'lib/cdo.rb', line 531

def setPersist(value)
  @persistent_tempfiles = value
end

#showFilesObject


548
549
550
# File 'lib/cdo.rb', line 548

def showFiles
  @_tempfiles.each {|f| print(f+" ") if f.kind_of? String}
end