Class: Siege::TempFile

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil) ⇒ TempFile

Returns a new instance of TempFile.



14
15
16
17
18
# File 'lib/siege/temp_file.rb', line 14

def initialize(content = nil)
  file  = [rand(6_251_983), '_', Time.now.to_i].join
  @name = File.expand_path(file, Dir.tmpdir)
  write(content) if content
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/siege/temp_file.rb', line 12

def name
  @name
end

Class Method Details

.with_content(content) ⇒ Object



7
8
9
# File 'lib/siege/temp_file.rb', line 7

def with_content(content)
  new content
end

Instance Method Details

#deleteObject



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

def delete
  File.delete(@name)
end

#readObject



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

def read
  File.read(@name)
end

#write(contents) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/siege/temp_file.rb', line 28

def write(contents)
  file = File.new(@name, 'w')
  file.puts(contents)
  file.close

  at_exit { File.delete(@name) }
end