class BitStruct::Field
Attributes
Default value.
Display name of field (used for printing).
Format for printed value of field.
Length of field in bits.
Name of field (used for its accessors).
Offset of field in bits.
Options, such as :default (varies for each field subclass). In general, options can be provided as strings or as symbols.
Length of field in bits.
Public Class Methods
Used in describe.
# File lib/bit-struct/field.rb 36 def self.class_name 37 @class_name ||= name[/\w+$/] 38 end
Subclasses can override this to define a default for all fields of this class, not just the one currently being added to a BitStruct
class, a “default default” if you will. The global default, if default
returns nil, is to fill the field with zero. Most field classes just let this default stand. The default can be overridden per-field when a BitStruct
class is defined.
# File lib/bit-struct/field.rb 33 def self.default; nil; end
Options are display_name, default, and format (subclasses of Field
may add other options).
# File lib/bit-struct/field.rb 67 def initialize(offset, length, name, opts = {}) 68 @offset, @length, @name, @options = 69 offset, length, name, opts 70 71 @display_name = opts[:display_name] || opts["display_name"] 72 @default = opts[:default] || opts["default"] || self.class.default 73 @format = opts[:format] || opts["format"] 74 end
Public Instance Methods
Used in describe. Can be overridden per-subclass, as in NestedField
.
# File lib/bit-struct/field.rb 41 def class_name 42 self.class.class_name 43 end
Yield the description of this field, as an array of 5 strings: byte offset, type, name, size, and description. The opts hash may have:
- :expand
-
if the value is true, expand complex fields
(Subclass implementations may yield more than once for complex fields.)
# File lib/bit-struct/field.rb 52 def describe opts 53 bits = size 54 if bits > 32 and bits % 8 == 0 55 len_str = "%dB" % (bits/8) 56 else 57 len_str = "%db" % bits 58 end 59 60 byte_offset = offset / 8 + (opts[:byte_offset] || 0) 61 62 yield ["@%d" % byte_offset, class_name, name, len_str, display_name] 63 end
Inspect the value of this field in the specified obj.
# File lib/bit-struct/field.rb 77 def inspect_in_object(obj, opts) 78 val = obj.send(name) 79 str = 80 begin 81 val.inspect_with_options(opts) 82 rescue NoMethodError 83 val.inspect 84 end 85 (f=@format) ? (f % str) : str 86 end
Normally, all fields show up in inspect, but some, such as padding, should not.
# File lib/bit-struct/field.rb 90 def inspectable?; true; end