class MxxRu::Util::Tmp_files

Class, responsible for creation and removal of temporary files. Temporary files are created in current folder. Temporary files are removed before application is exitted, if –mxx-keep-tmps option isn't set.

Public Class Methods

finalizer() click to toggle source
# File lib/mxx_ru/util.rb, line 247
def TmpFiles.finalizer
  @@names.each { |d|
      begin
        File.delete( d )
      rescue Exception => e
        puts e
      end
    }
end
new() click to toggle source
# File lib/mxx_ru/util.rb, line 235
def initialize
  if !Mode.instance.is_keep_tmps
    ObjectSpace.define_finalizer( self, Proc.new{ TmpFiles.finalizer } )
  end

  # Counter to ensure unique file names.
  @current = 0

  # Indicies stack in @@names for push and pop methods.
  @index_stack = Array.new
end

Public Instance Methods

create( a_content ) click to toggle source

Temporary file creation. The string given is stored to the temporary file. The name of temporary file is returned.

a_content

Should be an object of String type.

# File lib/mxx_ru/util.rb, line 262
def create( a_content )

  current = (@current += 1)
  file_name = "tmpmxx_ru.#{$$}.#{current}"
  file = File.new( file_name, "w" )
  @@names << file_name
  file << a_content
  file.close

  if Mode.instance.is_show_tmps
    puts "<<<[#{file_name}]\t #{a_content}>>>"
  end

  return file_name
end
pop() click to toggle source

Removing all files, which are stored in a file names vector starting from first element from @index_stack position.

# File lib/mxx_ru/util.rb, line 287
def pop()
  if !Mode.instance.is_keep_tmps
    index = @index_stack.pop
    while @@names.size > index
      file_to_delete = @@names.pop
      MxxRu::Util::delete_file( file_to_delete )
    end
  end
end
push() click to toggle source

Storing current vector size to the indicies stack.

# File lib/mxx_ru/util.rb, line 279
def push()
  if !Mode.instance.is_keep_tmps
    @index_stack.push( @@names.size() )
  end
end