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 Attribute Summary collapse

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



13
14
15
16
17
18
19
20
# File 'lib/rune/core/buffer.rb', line 13

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 Attribute Details

#dataString (readonly)

Note:

This attribute is read-only.

Returns the data contained in the buffer.

Returns:

  • (String)

    the data contained in the buffer.

Since:

  • 0.1.0



9
10
11
# File 'lib/rune/core/buffer.rb', line 9

def data
  @data
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



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

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

#empty?Boolean

Is the #data empty?

Returns:

  • (Boolean)

    true if the #data is empty, false otherwise.

Since:

  • 0.1.0



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

def empty?
  @data.empty?
end

#lengthInteger

Returns the limit of the buffer.

Returns:

  • (Integer)

    the limit of the buffer.

Since:

  • 0.1.0



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

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



61
62
63
64
65
66
67
68
69
# File 'lib/rune/core/buffer.rb', line 61

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



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

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

#snapshotString

Returns a snapshot of the buffer.

Returns:

  • (String)

    a snapshot of the buffer.

Since:

  • 0.1.0



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

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



54
55
56
# File 'lib/rune/core/buffer.rb', line 54

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