Class: MxxRu::Util::TmpFiles

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mxx_ru/util.rb

Overview

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.

Constant Summary collapse

@@names =

Temporary file names.

Array.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTmpFiles

Returns a new instance of TmpFiles.



235
236
237
238
239
240
241
242
243
244
245
# 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

Class Method Details

.finalizerObject



247
248
249
250
251
252
253
254
255
# 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

Instance Method Details

#create(a_content) ⇒ Object

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.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# 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

#popObject

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



287
288
289
290
291
292
293
294
295
# 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

#pushObject

Storing current vector size to the indicies stack.



279
280
281
282
283
# File 'lib/mxx_ru/util.rb', line 279

def push()
  if !Mode.instance.is_keep_tmps
    @index_stack.push( @@names.size() )
  end
end