Class: Tracksperanto::BufferIO

Inherits:
IOWrapper show all
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

Constructor Details

#initializeBufferIO

Returns a new instance of BufferIO.



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

def initialize
  @backing_buffer = StringIO.new
end

Instance Method Details

#close!Object



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

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

#file_backed?Boolean

Tells whether this one is on disk

Returns:

  • (Boolean)


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

def file_backed?
  @tempfile_in
end

#putc(c) ⇒ Object



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

def putc(c)
  super.tap { replace_with_tempfile_if_needed }
end

#puts(s) ⇒ Object



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

def puts(s)
  super.tap { 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.



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

def to_file
  replace_with_tempfile unless @tempfile_in
  flush
  self
end

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



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

def write(s)
  super.tap { replace_with_tempfile_if_needed }
end