class Kumogata::StringStream

Public Class Methods

new(&block) click to toggle source
# File lib/kumogata/string_stream.rb, line 2
def initialize(&block)
  @buf = StringScanner.new('')
  @block = block

  @fiber = Fiber.new do
    self.run
  end

  # Step to `yield`
  @fiber.resume
end

Public Instance Methods

close() click to toggle source
# File lib/kumogata/string_stream.rb, line 34
def close
  self.each_line
  @block.call(@buf.rest) if @buf.rest?
  @fiber.resume
end
each_line() click to toggle source
# File lib/kumogata/string_stream.rb, line 24
def each_line
  while (line = @buf.scan_until(/(\r\n|\r|\n)/))
    @block.call(line.chomp)
  end
end
push(chunk) click to toggle source
# File lib/kumogata/string_stream.rb, line 30
def push(chunk)
  @fiber.resume(chunk)
end
run() click to toggle source
# File lib/kumogata/string_stream.rb, line 14
def run
  loop do
    chunk = Fiber.yield
    break unless chunk

    @buf << chunk.to_s
    self.each_line
  end
end