Class: Protobuf::Rpc::Buffer
- Inherits:
-
Object
- Object
- Protobuf::Rpc::Buffer
- Defined in:
- lib/protobuf/rpc/buffer.rb
Constant Summary collapse
- MODES =
[:read, :write].freeze
- 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
rubocop:disable Style/AccessorMethodName.
-
#initialize(mode = :read) ⇒ Buffer
constructor
A new instance of Buffer.
- #reading? ⇒ Boolean
-
#set_data(data) ⇒ Object
rubocop:disable Style/AccessorMethodName.
- #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
39 40 41 42 43 44 45 |
# File 'lib/protobuf/rpc/buffer.rb', line 39 def <<(data) @data << data if reading? get_data_size check_for_flush end end |
#flushed? ⇒ Boolean
60 61 62 |
# File 'lib/protobuf/rpc/buffer.rb', line 60 def flushed? @flush end |
#get_data_size ⇒ Object
rubocop:disable Style/AccessorMethodName
64 65 66 67 68 69 |
# File 'lib/protobuf/rpc/buffer.rb', line 64 def get_data_size # rubocop:disable Style/AccessorMethodName if @size == 0 || @data.match(SIZE_REGEX) sliced_size = @data.slice!(SIZE_REGEX) @size = sliced_size.delete('-').to_i unless sliced_size.nil? end end |
#reading? ⇒ Boolean
52 53 54 |
# File 'lib/protobuf/rpc/buffer.rb', line 52 def reading? mode == :read end |
#set_data(data) ⇒ Object
rubocop:disable Style/AccessorMethodName
47 48 49 50 |
# File 'lib/protobuf/rpc/buffer.rb', line 47 def set_data(data) # rubocop:disable Style/AccessorMethodName @data = data.to_s @size = @data.size end |
#write(force_mode = true) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/protobuf/rpc/buffer.rb', line 28 def write(force_mode = true) if force_mode && reading? self.mode = :write elsif !force_mode && reading? fail 'You chose to write the buffer when in read mode' end @size = @data.length "#{@size}-#{@data}" end |
#writing? ⇒ Boolean
56 57 58 |
# File 'lib/protobuf/rpc/buffer.rb', line 56 def writing? mode == :write end |