Class: BinData::Virtual

Inherits:
BasePrimitive 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.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 Attribute Summary

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from BasePrimitive

#<=>, #assign, bit_aligned, #clear?, #do_read_with_hook, #eql?, #hash, #initialize_instance, #initialize_shared_instance, #method_missing, #respond_to?, #snapshot, #trace_value, turn_off_tracing, turn_on_tracing, #value, #value=

Methods inherited from Base

#==, #=~, #abs_offset, arg_processor, auto_call_delayed_io, bindata_name, #clear, #debug_name, #eval_parameter, #get_parameter, #has_parameter?, #initialize_instance, #initialize_with_warning, #inspect, #lazy_evaluator, #new, #num_bytes, #pretty_print, #read, read, register_subclasses, #rel_offset, #safe_respond_to?, #to_binary_s, #to_hex, #to_s, unregister_self, #write

Methods included from AcceptedParametersPlugin

#accepted_parameters, #default_parameters, #mandatory_parameters, #mutually_exclusive_parameters, #optional_parameters

Methods included from CheckOrAdjustOffsetPlugin

included, #initialize_shared_instance

Methods included from RegisterNamePlugin

included, #initialize_shared_instance

Methods included from Framework

#assign, #bit_aligned?, #clear?, #debug_name_of, #offset_of, #snapshot

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BinData::BasePrimitive

Instance Method Details

#do_num_bytesObject



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

def do_num_bytes
  0.0
end

#do_read(io) ⇒ Object



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

def do_read(io)
end

#do_write(io) ⇒ Object



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

def do_write(io)
end

#sensible_defaultObject



43
44
45
# File 'lib/bindata/virtual.rb', line 43

def sensible_default
  nil
end