Class: Riml::WarningBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/riml/warning_buffer.rb

Overview

Thread-safe output buffer. Used internally for all Riml warnings. Only one of these objects exists during compile.

Constant Summary collapse

WRITE_LOCK =

This class acts as a singleton, so no instance-level mutexes are required. This facilitates locking both class and instance methods with a single mutex.

Mutex.new
WARNING_FMT =
"Warning: %s"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*warnings) ⇒ WarningBuffer

Returns a new instance of WarningBuffer.



23
24
25
# File 'lib/riml/warning_buffer.rb', line 23

def initialize(*warnings)
  @buffer = warnings
end

Class Attribute Details

.streamObject

Returns the value of attribute stream.



15
16
17
# File 'lib/riml/warning_buffer.rb', line 15

def stream
  @stream
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



21
22
23
# File 'lib/riml/warning_buffer.rb', line 21

def buffer
  @buffer
end

Instance Method Details

#<<(warning) ⇒ Object Also known as: push



27
28
29
# File 'lib/riml/warning_buffer.rb', line 27

def <<(warning)
  WRITE_LOCK.synchronize { buffer << warning }
end

#clearObject



41
42
43
# File 'lib/riml/warning_buffer.rb', line 41

def clear
  WRITE_LOCK.synchronize { buffer.clear }
end

#flushObject



32
33
34
35
36
37
38
39
# File 'lib/riml/warning_buffer.rb', line 32

def flush
  WRITE_LOCK.synchronize do
    stream = self.class.stream
    buffer.each { |w| stream.puts WARNING_FMT % w }
    buffer.clear
    stream.flush
  end
end