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.



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

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



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

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

Instance Method Details

#doneObject



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

def done
  @file.close
  @file.unlink
end

#pathObject



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

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