Class: BinData::Virtual

Inherits:
BasePrimitive
  • Object
show all
Defined in:
lib/bindata/virtual.rb

Overview

A virtual field is one that is neither read, written nor occupies space in the data stream. It is used to make assertions or as a convenient label for determining offsets or storing values.

require 'bindata'

class A < BinData::Record
string  :a, read_length: 5
string  :b, read_length: 5
virtual :c, assert: -> { a == b }
end

obj = A.read("abcdeabcde")
obj.a #=> "abcde"
obj.c.rel_offset #=> 10

obj = A.read("abcdeABCDE") #=> BinData::ValidityError: assertion failed for obj.c

Parameters

Parameters may be provided at initialisation to control the behaviour of an object. These params include those for BinData::Base as well as:

[:assert] Raise an error when reading or assigning if the value of this evaluated parameter is false. [:value] The virtual object will always have this value.

Instance Method Summary collapse

Instance Method Details

#do_num_bytesObject



36
37
38
# File 'lib/bindata/virtual.rb', line 36

def do_num_bytes
  0.0
end

#do_read(io) ⇒ Object



32
# File 'lib/bindata/virtual.rb', line 32

def do_read(io); end

#do_write(io) ⇒ Object



34
# File 'lib/bindata/virtual.rb', line 34

def do_write(io); end

#sensible_defaultObject



40
41
42
# File 'lib/bindata/virtual.rb', line 40

def sensible_default
  nil
end