Class: RuneRb::Core::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/rune/core/buffer.rb

Overview

A Buffer encapsulates raw data in a String instance. Depending on the mode, the buffer can be read from or written to.

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(mode: 'rw') ⇒ Buffer

Constructs a new Buffer instance.

Parameters:

  • mode (String) (defaults to: 'rw')

    the mode of the buffer.

Raises:

  • (StandardError)

Since:

  • 0.1.0



8
9
10
11
12
13
14
15
# File 'lib/rune/core/buffer.rb', line 8

def initialize(mode: 'rw')
  @data = String.new
  @mode = mode
  raise StandardError, 'Buffer mode must include "r" or "w".' unless @mode.include?('r') || @mode.include?('w')

  enable_readable if @mode.include?('r')
  enable_writeable if @mode.include?('w')
end

Instance Method Details

#<<(data) ⇒ Object

Adds raw data to the buffer. Flips the buffer if it is readable.

Parameters:

  • data (String)

    the data to add.

Since:

  • 0.1.0



37
38
39
# File 'lib/rune/core/buffer.rb', line 37

def <<(data)
  @data << data
end

#empty?Boolean

Is the Buffer#data empty?

Returns:

  • (Boolean)

    true if the Buffer#data is empty, false otherwise.

Since:

  • 0.1.0



25
26
27
# File 'lib/rune/core/buffer.rb', line 25

def empty?
  @data.empty?
end

#lengthInteger

Returns the limit of the buffer.

Returns:

  • (Integer)

    the limit of the buffer.

Since:

  • 0.1.0



31
32
33
# File 'lib/rune/core/buffer.rb', line 31

def length
  @data.bytesize
end

#mutate(value, mutation) ⇒ Object

Mutates the value according to the passed mutation

Parameters:

  • value (Integer)

    the value to mutate

  • mutation (Symbol)

    the mutation to apply to the value.

Since:

  • 0.1.0



56
57
58
59
60
61
62
63
64
# File 'lib/rune/core/buffer.rb', line 56

def mutate(value, mutation)
  case mutation
  when :STD then value
  when :ADD then value + 128
  when :NEG then -value
  when :SUB then value - 128
  else mutate(value, :STD)
  end
end

#readable?Boolean

Is the <Buffer> instance readable?

Returns:

  • (Boolean)

    true if the <Buffer> instance is readable, false otherwise.

Since:

  • 0.1.0



43
44
45
# File 'lib/rune/core/buffer.rb', line 43

def readable?
  @mode.include?('r')
end

#snapshotString

Returns a snapshot of the buffer.

Returns:

  • (String)

    a snapshot of the buffer.

Since:

  • 0.1.0



19
20
21
# File 'lib/rune/core/buffer.rb', line 19

def snapshot
  @data.dup
end

#writeable?Boolean

Is the <Buffer> instance writeable?

Returns:

  • (Boolean)

    true if the <Buffer> instance is writeable, false otherwise.

Since:

  • 0.1.0



49
50
51
# File 'lib/rune/core/buffer.rb', line 49

def writeable?
  @mode.include?('w')
end