class Protobuf::Rpc::Buffer

Constants

MODES
SIZE_REGEX

constantize this so we don’t re-initialize the regex every time we need it

Attributes

data[RW]
mode[RW]
size[RW]

Public Class Methods

new(mode = :read) click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 12
def initialize(mode = :read)
  @flush = false
  @data = ""
  @size = 0
  self.mode = mode
end

Public Instance Methods

<<(data) click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 38
def <<(data)
  @data << data
  if reading?
    get_data_size
    check_for_flush
  end
end
flushed?() click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 59
def flushed?
  @flush
end
get_data_size() click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 63
def get_data_size # rubocop:disable Style/AccessorMethodName
  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
mode=(mode) click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 19
def mode=(mode)
  if MODES.include?(mode)
    @mode = mode
  else
    @mode = :read
  end
end
reading?() click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 51
def reading?
  mode == :read
end
set_data(data) click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 46
def set_data(data) # rubocop:disable Style/AccessorMethodName
  @data = data.to_s
  @size = @data.size
end
write(force_mode = true) click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 27
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?() click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 55
def writing?
  mode == :write
end

Private Instance Methods

check_for_flush() click to toggle source
# File lib/protobuf/rpc/buffer.rb, line 72
def check_for_flush
  @flush = true if !@size.nil? && @data.length == @size
end