File: virtual.rb

package info (click to toggle)
ruby-bindata 2.4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 600 kB
  • sloc: ruby: 8,566; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,203 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require "bindata/base"

module BinData
  # 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:
  #
  # [<tt>:assert</tt>]    Raise an error when reading or assigning if the value
  #                       of this evaluated parameter is false.
  # [<tt>:value</tt>]     The virtual object will always have this value.
  #
  class Virtual < BinData::BasePrimitive

    def do_read(io)
    end

    def do_write(io)
    end

    def do_num_bytes
      0.0
    end

    def sensible_default
      nil
    end
  end
end