Class: RuneRb::Core::Buffer
- Inherits:
-
Object
- Object
- RuneRb::Core::Buffer
- 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.
Instance Attribute Summary collapse
-
#data ⇒ String
readonly
The data contained in the buffer.
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Adds raw data to the buffer.
-
#empty? ⇒ Boolean
Is the #data empty?.
-
#initialize(mode: 'rw') ⇒ Buffer
constructor
Constructs a new Buffer instance.
-
#length ⇒ Integer
Returns the limit of the buffer.
-
#mutate(value, mutation) ⇒ Object
Mutates the value according to the passed mutation.
-
#readable? ⇒ Boolean
Is the <Buffer> instance readable?.
-
#snapshot ⇒ String
Returns a snapshot of the buffer.
-
#writeable? ⇒ Boolean
Is the <Buffer> instance writeable?.
Constructor Details
#initialize(mode: 'rw') ⇒ Buffer
Constructs a new Buffer instance.
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
#data ⇒ String (readonly)
This attribute is read-only.
Returns the data contained in the buffer.
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.
42 43 44 |
# File 'lib/rune/core/buffer.rb', line 42 def <<(data) @data << data end |
#empty? ⇒ Boolean
Is the #data empty?
30 31 32 |
# File 'lib/rune/core/buffer.rb', line 30 def empty? @data.empty? end |
#length ⇒ Integer
Returns the limit of the buffer.
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
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?
48 49 50 |
# File 'lib/rune/core/buffer.rb', line 48 def readable? @mode.include?('r') end |
#snapshot ⇒ String
Returns a snapshot of the buffer.
24 25 26 |
# File 'lib/rune/core/buffer.rb', line 24 def snapshot @data.dup end |
#writeable? ⇒ Boolean
Is the <Buffer> instance writeable?
54 55 56 |
# File 'lib/rune/core/buffer.rb', line 54 def writeable? @mode.include?('w') end |