Sha256: bfcb56c15744b2d0263884f999cb8d5516a766c33359ea652135a66a74d78184

Contents?: true

Size: 605 Bytes

Versions: 15

Compression:

Stored size: 605 Bytes

Contents

# frozen_string_literal: true

module Shipit
  class LineBuffer
    SEPARATOR = "\n"

    def initialize(queue = "")
      @queue = queue.dup
    end

    def buffer(text, &block)
      @queue << text
      whole_lines.each(&block).tap { flush }
    end

    def empty?
      @queue.empty?
    end

    private

    def whole_lines
      whole? ? lines : lines[0..-2]
    end

    def flush
      whole? ? clear : @queue = lines.last
    end

    def whole?
      @queue.end_with?(SEPARATOR)
    end

    def lines
      @queue.split(SEPARATOR)
    end

    def clear
      @queue.clear
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
shipit-engine-0.39.0 lib/shipit/line_buffer.rb
shipit-engine-0.38.0 lib/shipit/line_buffer.rb
shipit-engine-0.37.0 lib/shipit/line_buffer.rb
shipit-engine-0.36.1 lib/shipit/line_buffer.rb
shipit-engine-0.36.0 lib/shipit/line_buffer.rb
shipit-engine-0.35.1 lib/shipit/line_buffer.rb
shipit-engine-0.35.0 lib/shipit/line_buffer.rb
shipit-engine-0.34.0 lib/shipit/line_buffer.rb
shipit-engine-0.33.0 lib/shipit/line_buffer.rb
shipit-engine-0.32.0 lib/shipit/line_buffer.rb
shipit-engine-0.31.0 lib/shipit/line_buffer.rb
shipit-engine-0.30.0 lib/shipit/line_buffer.rb
shipit-engine-0.29.0 lib/shipit/line_buffer.rb
shipit-engine-0.28.1 lib/shipit/line_buffer.rb
shipit-engine-0.28.0 lib/shipit/line_buffer.rb