Class: Protobuf::Rpc::Buffer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/protobuf/rpc/buffer.rb', line 5

def data
  @data
end

#modeObject

Returns the value of attribute mode.



5
6
7
# File 'lib/protobuf/rpc/buffer.rb', line 5

def mode
  @mode
end

#sizeObject

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

Returns:

  • (Boolean)


60
61
62
# File 'lib/protobuf/rpc/buffer.rb', line 60

def flushed?
  @flush
end

#get_data_sizeObject

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

Returns:

  • (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

Returns:

  • (Boolean)


56
57
58
# File 'lib/protobuf/rpc/buffer.rb', line 56

def writing?
  mode == :write
end