Class: Reactomatic::Buffer
- Inherits:
-
Object
- Object
- Reactomatic::Buffer
- Defined in:
- lib/reactomatic/buffer.rb
Instance Attribute Summary collapse
-
#max_length ⇒ Object
readonly
Returns the value of attribute max_length.
Instance Method Summary collapse
- #append(data) ⇒ Object
- #consume(length) ⇒ Object
- #empty? ⇒ Boolean
- #full? ⇒ Boolean
-
#initialize(opts = {}) ⇒ Buffer
constructor
A new instance of Buffer.
- #length ⇒ Object
- #read ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Buffer
5 6 7 8 9 10 |
# File 'lib/reactomatic/buffer.rb', line 5 def initialize(opts = {}) @max_length = opts[:max_length] @opts = opts @lock = Mutex.new @buffer = "" end |
Instance Attribute Details
#max_length ⇒ Object (readonly)
Returns the value of attribute max_length.
3 4 5 |
# File 'lib/reactomatic/buffer.rb', line 3 def max_length @max_length end |
Instance Method Details
#append(data) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/reactomatic/buffer.rb', line 12 def append(data) if @max_length.nil? || length + data.bytesize <= @max_length @buffer.concat(data) else raise BufferFull end end |
#consume(length) ⇒ Object
24 25 26 |
# File 'lib/reactomatic/buffer.rb', line 24 def consume(length) return @buffer.slice!(0...length) end |
#empty? ⇒ Boolean
37 38 39 |
# File 'lib/reactomatic/buffer.rb', line 37 def empty? return @buffer.empty? end |
#full? ⇒ Boolean
32 33 34 35 |
# File 'lib/reactomatic/buffer.rb', line 32 def full? return false if @max_length.nil? return @buffer.bytesize >= @max_length end |
#length ⇒ Object
28 29 30 |
# File 'lib/reactomatic/buffer.rb', line 28 def length return @buffer.bytesize end |
#read ⇒ Object
20 21 22 |
# File 'lib/reactomatic/buffer.rb', line 20 def read return @buffer end |