Class: TempfileFor::Tempfile

Inherits:
Tempfile show all
Defined in:
lib/tempfile_for/tempfile.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tempfile

blank, for

Class Method Details

.build(options = {}) ⇒ Object



6
7
8
# File 'lib/tempfile_for/tempfile.rb', line 6

def self.build(options = {})
  open options[:suffix] ? ["tempfile", options[:suffix]] : "tempfile", :encoding => options[:encoding]
end

Instance Method Details

#copy(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/tempfile_for/tempfile.rb', line 10

def copy(options = {})
  tempfile = self.class.build(options)

  File.open(path, :encoding => options[:encoding]) { |stream| tempfile.write_ext stream }

  tempfile.rewind
  tempfile
end

#write_ext(io_or_data) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/tempfile_for/tempfile.rb', line 19

def write_ext(io_or_data)
  if io_or_data.respond_to?(:read)
    while data = io_or_data.read(1024)
      write data
    end
  else
    write io_or_data
  end
end