Class: CommandUtils::LineBuffer
- Inherits:
- 
      Object
      
        - Object
- CommandUtils::LineBuffer
 
- Defined in:
- lib/command_utils/line_buffer.rb
Overview
Line buffers writes, and calls method at each line
Instance Method Summary collapse
- 
  
    
      #flush  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Send all buffer to method. 
- 
  
    
      #initialize(method, prefix = '')  ⇒ LineBuffer 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Receive a method to call passing each line received. 
- 
  
    
      #write(str)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Receive a new chunk, and if a line is formed, call method. 
Constructor Details
#initialize(method, prefix = '') ⇒ LineBuffer
Receive a method to call passing each line received. Optionally, specify a prefix.
| 6 7 8 9 10 | # File 'lib/command_utils/line_buffer.rb', line 6 def initialize method, prefix='' @method = method @prefix = prefix @buffer = nil end | 
Instance Method Details
#flush ⇒ Object
Send all buffer to method
| 29 30 31 32 33 | # File 'lib/command_utils/line_buffer.rb', line 29 def flush return unless @buffer @method.call(@prefix + @buffer) @buffer = nil end | 
#write(str) ⇒ Object
Receive a new chunk, and if a line is formed, call method.
| 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # File 'lib/command_utils/line_buffer.rb', line 13 def write str @buffer ||= '' @buffer += str return unless @buffer.include? "\n" lines = @buffer.split("\n") @buffer = if @buffer.match("\n$") nil else lines.pop end lines.each do |line| @method.call(@prefix + line) end end |