Class: Chan::Bytes
- Inherits:
-
Object
- Object
- Chan::Bytes
- Defined in:
- lib/xchan/bytes.rb
Overview
Chan::Bytes represents a collection of byte counts for each object stored on a channel. When an object is written to a channel, the collection increases in size, and when an object is read from a channel, the collection decreases in size.
Instance Method Summary collapse
-
#initialize(tmpdir) ⇒ Chan::Bytes
constructor
-
#unshift(len) ⇒ void
Adds a count to the start of the collection.
-
#push(len) ⇒ void
Adds a count to the end of the collection.
-
#shift ⇒ Integer
Removes a count from the start of the collection.
-
#size ⇒ Integer
Returns the number of objects in the collection.
-
#close ⇒ void
Close the underlying IO.
Constructor Details
#initialize(tmpdir) ⇒ Chan::Bytes
17 18 19 20 21 |
# File 'lib/xchan/bytes.rb', line 17 def initialize(tmpdir) @io = Chan.temporary_file(%w[bytes .json], tmpdir:) @io.sync = true write(@io, []) end |
Instance Method Details
#unshift(len) ⇒ void
This method returns an undefined value.
Adds a count to the start of the collection
28 29 30 31 32 33 34 |
# File 'lib/xchan/bytes.rb', line 28 def unshift(len) return 0 if len.nil? || len.zero? bytes = read(@io) bytes.unshift(len) write(@io, bytes) len end |
#push(len) ⇒ void
This method returns an undefined value.
Adds a count to the end of the collection
41 42 43 44 45 46 47 |
# File 'lib/xchan/bytes.rb', line 41 def push(len) return 0 if len.nil? || len.zero? bytes = read(@io) bytes.push(len) write(@io, bytes) len end |
#shift ⇒ Integer
Removes a count from the start of the collection
53 54 55 56 57 58 59 |
# File 'lib/xchan/bytes.rb', line 53 def shift bytes = read(@io) return 0 if bytes.size.zero? len = bytes.shift write(@io, bytes) len end |
#size ⇒ Integer
Returns the number of objects in the collection
64 65 66 |
# File 'lib/xchan/bytes.rb', line 64 def size read(@io).size end |
#close ⇒ void
This method returns an undefined value.
Close the underlying IO
71 72 73 |
# File 'lib/xchan/bytes.rb', line 71 def close @io.close end |