Class: Tempfile

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

Direct Known Subclasses

TempfileFor::Tempfile

Class Method Summary collapse

Class Method Details

.blank(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tempfile_for.rb', line 16

def self.blank(options = {})
  tempfile = TempfileFor::Tempfile.build(options)

  yield tempfile if block_given?

  tempfile.flush

  options[:read] != false ? File.read(tempfile.path, :encoding => options[:encoding]) : tempfile.copy(options)
ensure
  tempfile.close!
end

.for(io_or_data, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/tempfile_for.rb', line 7

def self.for(io_or_data, options = {})
  blank options.merge(:encoding => options[:encoding] || io_or_data.encoding) do |tempfile|
    tempfile.write_ext io_or_data
    tempfile.flush

    yield tempfile if block_given?
  end
end