Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying and Tempfile conversion.
Methods
Public Instance methods
Copies one read-able object from one place to another in blocks, obviating the need to load the whole thing into memory. Defaults to 8k blocks. If this module is included in both StringIO and Tempfile, then either can have its data copied anywhere else without typing worries or memory overhead worries. Returns a File if a String is passed in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
[ show source ]
# File lib/data_base/attachment/iostream.rb, line 19
19: def stream_to(path_or_file, in_blocks_of = 8192)
20: dstio = case path_or_file
21: when String then File.new(path_or_file, "wb+")
22: when IO then path_or_file
23: when Tempfile then path_or_file
24: end
25: buffer = ""
26: self.rewind
27: while self.read(in_blocks_of, buffer) do
28: dstio.write(buffer)
29: end
30: dstio.rewind
31: dstio
32: end
Returns a Tempfile containing the contents of the readable object.
[ show source ]
# File lib/data_base/attachment/iostream.rb, line 8
8: def to_tempfile
9: tempfile = Tempfile.new("stream")
10: tempfile.binmode
11: self.stream_to(tempfile)
12: end