Class: BitStruct::NestedField

Inherits:
Field
  • Object
show all
Defined in:
lib/bit-struct/nested-field.rb

Overview

Class for nesting a BitStruct as a field within another BitStruct. Declared with BitStruct.nest.

Instance Attribute Summary

Attributes inherited from Field

#default, #display_name, #format, #length, #name, #offset, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Field

default, #inspect_in_object, #inspectable?

Constructor Details

#initialize(*args) ⇒ NestedField

Returns a new instance of NestedField.



7
8
9
# File 'lib/bit-struct/nested-field.rb', line 7

def initialize(*args)
  super
end

Class Method Details

.class_nameObject

Used in describe.



12
13
14
# File 'lib/bit-struct/nested-field.rb', line 12

def self.class_name
  @class_name ||= "nest"
end

Instance Method Details

#add_accessors_to(cl, attr = name) ⇒ Object

:nodoc:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bit-struct/nested-field.rb', line 35

def add_accessors_to(cl, attr = name) # :nodoc:
  unless offset % 8 == 0
    raise ArgumentError,
      "Bad offset, #{offset}, for nested field #{name}." +
      " Must be multiple of 8."
  end
  
  unless length % 8 == 0
    raise ArgumentError,
      "Bad length, #{length}, for nested field #{name}." +
      " Must be multiple of 8."
  end
  
  offset_byte = offset / 8
  length_byte = length / 8
  last_byte = offset_byte + length_byte - 1
  byte_range = offset_byte..last_byte

  nc = nested_class
  
  cl.class_eval do
    define_method attr do ||
      nc.new(self[byte_range])
    end

    define_method "#{attr}=" do |val|
      if val.length != length_byte
        raise ArgumentError, "Size mismatch in nested struct assignment " +
          "to #{attr} with value #{val.inspect}"
      end
      
      if val.class != nc
        warn "Type mismatch in nested struct assignment " +
          "to #{attr} with value #{val.inspect}"
      end
      
      self[byte_range] = val
    end
  end
end

#class_nameObject



16
17
18
# File 'lib/bit-struct/nested-field.rb', line 16

def class_name
  @class_name ||= nested_class.name[/\w+$/]
end

#describe(opts) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/bit-struct/nested-field.rb', line 24

def describe opts
  if opts[:expand]
    opts = opts.dup
    opts[:byte_offset] = offset / 8
    opts[:omit_header] = opts[:omit_footer] = true
    nested_class.describe(nil, opts) {|desc| yield desc}
  else
    super
  end
end

#nested_classObject



20
21
22
# File 'lib/bit-struct/nested-field.rb', line 20

def nested_class
  @nested_class ||= options[:nested_class] || options["nested_class"]
end