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.



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

def initialize
  @backing_buffer = StringIO.new
end

Instance Method Details

#close!Object



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

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

#file_backed?Boolean

Tells whether this one is on disk

Returns:

  • (Boolean)


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

def file_backed?
  @tempfile_in
end

#putc(c) ⇒ Object



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

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

#puts(s) ⇒ Object



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

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.



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

def to_file
  replace_with_tempfile unless @tempfile_in
  flush
  self
end

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



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

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