Class: TN::TempFile

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

Instance Method Summary collapse

Constructor Details

#initialize(content: nil, file: nil, name: 'tanga') ⇒ TempFile

Returns a new instance of TempFile.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tn/temp_file.rb', line 4

def initialize(content:nil, file:nil, name: 'tanga')
  extension = File.extname(name)
  file_name = SecureRandom.hex
  @file = file || ::Tempfile.new([file_name, extension])
  @file.binmode
  if content
    @file.write(content) if content
    @file.close
  end

  if block_given?
    yield(self)
    done
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



20
21
22
# File 'lib/tn/temp_file.rb', line 20

def method_missing(method, *args, &block)
  @file.send(method, *args, &block)
end

Instance Method Details

#doneObject



28
29
30
31
# File 'lib/tn/temp_file.rb', line 28

def done
  @file.close
  @file.unlink
end

#pathObject



24
25
26
# File 'lib/tn/temp_file.rb', line 24

def path
  File.absolute_path(@file.path)
end