Class: ConfCtl::LineBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/line_buffer.rb

Overview

Feed string data and get output as lines

Instance Method Summary collapse

Constructor Details

#initialize {|line| ... } ⇒ LineBuffer

If instantiated with a block, the block is invoked for each read line

Yield Parameters:

  • line (String)


6
7
8
9
# File 'lib/confctl/line_buffer.rb', line 6

def initialize(&block)
  @buffer = ''
  @block = block
end

Instance Method Details

#<<(str) ⇒ Object

Feed string

Parameters:

  • str (String)


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/confctl/line_buffer.rb', line 13

def <<(str)
  buffer << str
  return if block.nil?

  loop do
    out_line = read_line
    break if out_line.nil?

    block.call(out_line)
  end
end

#flushString

Return the buffer’s contents and flush it

If block was given to ConfCtl::LineBuffer, it will be invoked with the buffer contents.

Returns:

  • (String)


42
43
44
45
46
47
# File 'lib/confctl/line_buffer.rb', line 42

def flush
  ret = buffer.clone
  buffer.clear
  block.call(ret) if block
  ret
end

#read_lineString?

Read one line if there is one

Returns:

  • (String, nil)


27
28
29
30
31
32
33
34
# File 'lib/confctl/line_buffer.rb', line 27

def read_line
  nl = buffer.index("\n")
  return if nl.nil?

  line = buffer[0..nl]
  @buffer = buffer[nl + 1..]
  line
end