Sha256: 461294d20c657a223de7dd6084a3d065a17a80c35c129eab8edf6111ae692e29

Contents?: true

Size: 601 Bytes

Versions: 36

Compression:

Stored size: 601 Bytes

Contents

class Kumogata::StringStream
  def initialize(&block)
    @buf = StringScanner.new('')
    @block = block

    @fiber = Fiber.new do
      self.run
    end

    # Step to `yield`
    @fiber.resume
  end

  def run
    loop do
      chunk = Fiber.yield
      break unless chunk

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

  def each_line
    while (line = @buf.scan_until(/(\r\n|\r|\n)/))
      @block.call(line.chomp)
    end
  end

  def push(chunk)
    @fiber.resume(chunk)
  end

  def close
    self.each_line
    @block.call(@buf.rest) if @buf.rest?
    @fiber.resume
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
kumogata-0.5.12 lib/kumogata/string_stream.rb
kumogata-0.5.11 lib/kumogata/string_stream.rb
kumogata-0.5.10 lib/kumogata/string_stream.rb
kumogata-0.5.9 lib/kumogata/string_stream.rb
kumogata-0.5.8 lib/kumogata/string_stream.rb
kumogata-0.5.7 lib/kumogata/string_stream.rb
kumogata-0.5.6 lib/kumogata/string_stream.rb
kumogata-0.5.5 lib/kumogata/string_stream.rb
kumogata-0.5.4 lib/kumogata/string_stream.rb
kumogata-0.5.3 lib/kumogata/string_stream.rb
kumogata-0.5.2 lib/kumogata/string_stream.rb
kumogata-0.5.2.beta2 lib/kumogata/string_stream.rb
kumogata-0.5.2.beta lib/kumogata/string_stream.rb
kumogata-0.5.1 lib/kumogata/string_stream.rb
kumogata-0.5.0 lib/kumogata/string_stream.rb
kumogata-0.4.19 lib/kumogata/string_stream.rb
kumogata-0.4.18 lib/kumogata/string_stream.rb
kumogata-0.4.17 lib/kumogata/string_stream.rb
kumogata-0.4.16 lib/kumogata/string_stream.rb
kumogata-0.4.15 lib/kumogata/string_stream.rb