class Proclib::OutputHandler::LineBuffer

Calls its given callback with each line in the input written to the buffer

Constants

MAX_SIZE
MaxSizeExceeded
NEWLINE
SIZE_ERROR_MESSAGE

Attributes

buf[RW]
callback[R]

Public Class Methods

new(&blk) click to toggle source
# File lib/proclib/output_handler.rb, line 17
def initialize(&blk)
  @buf = String.new
  @callback = blk
end

Public Instance Methods

flush() click to toggle source
# File lib/proclib/output_handler.rb, line 36
def flush
  callback.call(buf + "\n") unless buf.empty?
end
write(str) click to toggle source
# File lib/proclib/output_handler.rb, line 22
def write(str)
  buf << str

  while buf.include?(NEWLINE)
    idx = buf.index(NEWLINE)
    callback.call(buf[0..(idx - 1)] + NEWLINE)
    self.buf = (buf[(idx + 1)..-1] || String.new)
  end

  if buf.bytesize > MAX_SIZE
    raise MaxSizeExceeded, SIZE_ERROR_MESSAGE
  end
end