Class: Protobuf::Rpc::Buffer
- Inherits:
-
Object
- Object
- Protobuf::Rpc::Buffer
- Defined in:
- lib/protobuf/rpc/buffer.rb
Constant Summary collapse
- MODES =
[:read, :write]
- SIZE_REGEX =
constantize this so we don't re-initialize the regex every time we need it
/^\d+-/
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #flushed? ⇒ Boolean
- #get_data_size ⇒ Object
-
#initialize(mode = :read) ⇒ Buffer
constructor
A new instance of Buffer.
- #reading? ⇒ Boolean
- #set_data(data) ⇒ Object
- #write(force_mode = true) ⇒ Object
- #writing? ⇒ Boolean
Constructor Details
#initialize(mode = :read) ⇒ Buffer
Returns a new instance of Buffer.
12 13 14 15 16 17 |
# File 'lib/protobuf/rpc/buffer.rb', line 12 def initialize(mode=:read) @flush = false @data = "" @size = 0 self.mode = mode end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/protobuf/rpc/buffer.rb', line 5 def data @data end |
#mode ⇒ Object
Returns the value of attribute mode.
5 6 7 |
# File 'lib/protobuf/rpc/buffer.rb', line 5 def mode @mode end |
#size ⇒ Object
Returns the value of attribute size.
5 6 7 |
# File 'lib/protobuf/rpc/buffer.rb', line 5 def size @size end |
Instance Method Details
#<<(data) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/protobuf/rpc/buffer.rb', line 38 def <<(data) @data << data if reading? get_data_size check_for_flush end end |
#flushed? ⇒ Boolean
59 60 61 |
# File 'lib/protobuf/rpc/buffer.rb', line 59 def flushed? @flush end |
#get_data_size ⇒ Object
63 64 65 66 67 68 |
# File 'lib/protobuf/rpc/buffer.rb', line 63 def get_data_size if @size == 0 || @data.match(SIZE_REGEX) sliced_size = @data.slice!(SIZE_REGEX) @size = sliced_size.gsub('-', '').to_i unless sliced_size.nil? end end |
#reading? ⇒ Boolean
51 52 53 |
# File 'lib/protobuf/rpc/buffer.rb', line 51 def reading? mode == :read end |
#set_data(data) ⇒ Object
46 47 48 49 |
# File 'lib/protobuf/rpc/buffer.rb', line 46 def set_data(data) @data = data.to_s @size = @data.size end |
#write(force_mode = true) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/protobuf/rpc/buffer.rb', line 27 def write(force_mode=true) if force_mode and reading? mode = :write elsif not force_mode and reading? raise = 'You chose to write the buffer when in read mode' end @size = @data.length "#{@size}-#{@data}" end |
#writing? ⇒ Boolean
55 56 57 |
# File 'lib/protobuf/rpc/buffer.rb', line 55 def writing? mode == :write end |