Class: Tracksperanto::BufferIO

Inherits:
IOWrapper show all
Includes:
Returning
Defined in:
lib/tracksperanto/buffer_io.rb

Overview

BufferIO is used for writing big segments of text. It works like a StringIO, but when the size of the underlying string buffer exceeds MAX_IN_MEM_BYTES the string will be flushed to disk and it automagically becomes a Tempfile

Constant Summary collapse

MAX_IN_MEM_BYTES =
5_000_000

Constants inherited from IOWrapper

IOWrapper::IO_METHODS

Instance Attribute Summary

Attributes inherited from IOWrapper

#backing_buffer

Instance Method Summary collapse

Methods included from Returning

#returning

Constructor Details

#initializeBufferIO

Returns a new instance of BufferIO.



19
20
21
# File 'lib/tracksperanto/buffer_io.rb', line 19

def initialize
  @backing_buffer = StringIO.new
end

Instance Method Details

#close!Object



36
37
38
39
# File 'lib/tracksperanto/buffer_io.rb', line 36

def close!
  @backing_buffer.close! if @tempfile_in
  @backing_buffer = nil
end

#file_backed?Boolean

Tells whether this one is on disk

Returns:

  • (Boolean)


50
51
52
# File 'lib/tracksperanto/buffer_io.rb', line 50

def file_backed?
  @tempfile_in
end

#putc(c) ⇒ Object



32
33
34
# File 'lib/tracksperanto/buffer_io.rb', line 32

def putc(c)
  returning(super) { replace_with_tempfile_if_needed }
end

#puts(s) ⇒ Object



28
29
30
# File 'lib/tracksperanto/buffer_io.rb', line 28

def puts(s)
  returning(super) { replace_with_tempfile_if_needed }
end

#to_fileObject

Sometimes you just need to upgrade to a File forcibly (for example if you want) to have an object with many iterators sitting on it. We also flush here.



43
44
45
46
47
# File 'lib/tracksperanto/buffer_io.rb', line 43

def to_file
  replace_with_tempfile unless @tempfile_in
  flush
  self
end

#write(s) ⇒ Object Also known as: <<



23
24
25
# File 'lib/tracksperanto/buffer_io.rb', line 23

def write(s)
  returning(super) { replace_with_tempfile_if_needed }
end