Class: Tracksperanto::BufferIO

Inherits:
IO
  • Object
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

Instance Method Summary collapse

Methods included from Returning

#returning

Constructor Details

#initializeBufferIO

Returns a new instance of BufferIO.



12
13
14
# File 'lib/tracksperanto/buffer_io.rb', line 12

def initialize
  __setobj__(StringIO.new)
end

Instance Method Details

#close!Object



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

def close!
  __getobj__.close! if @tempfile_in
  __setobj__(nil)
end

#file_backed?Boolean

Tells whether this one is on disk

Returns:

  • (Boolean)


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

def file_backed?
  @tempfile_in
end

#putc(c) ⇒ Object



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

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

#puts(s) ⇒ Object



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

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.



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

def to_file
  replace_with_tempfile unless @tempfile_in
  flush
  self
end

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



16
17
18
# File 'lib/tracksperanto/buffer_io.rb', line 16

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