Class: Vx::Common::OutputBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/vx/common/output_buffer.rb

Defined Under Namespace

Classes: ClosedBuffer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval = 1, &block) ⇒ OutputBuffer

Returns a new instance of OutputBuffer.



9
10
11
12
13
14
15
16
17
# File 'lib/vx/common/output_buffer.rb', line 9

def initialize(interval = 1, &block)
  @interval = interval.to_f
  @buffer   = ""
  @write    = block
  @mutex    = Mutex.new
  @closed   = false

  start_watching
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



7
8
9
# File 'lib/vx/common/output_buffer.rb', line 7

def interval
  @interval
end

Instance Method Details

#<<(str) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/vx/common/output_buffer.rb', line 19

def << (str)
  closed!

  @mutex.synchronize do
    @buffer << str
  end
end

#closeObject



27
28
29
30
# File 'lib/vx/common/output_buffer.rb', line 27

def close
  @closed = true
  @thread.join
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/vx/common/output_buffer.rb', line 37

def empty?
  @buffer.size == 0
end

#flushObject



32
33
34
35
# File 'lib/vx/common/output_buffer.rb', line 32

def flush
  closed!
  @mutex.synchronize { write }
end