Class: Protobuf::Rpc::Buffer

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

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



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

Returns:

  • (Boolean)


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

def flushed?
  @flush
end

#get_data_sizeObject



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

Returns:

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

Returns:

  • (Boolean)


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

def writing?
  mode == :write
end