Class: BitStruct::Field
- Inherits:
-
Object
- Object
- BitStruct::Field
- Defined in:
- lib/bit-struct/bit-struct.rb
Direct Known Subclasses
CharField, FloatField, NestedField, PadField, SignedField, TextField, UnsignedField, VectorField
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Default value.
-
#display_name ⇒ Object
readonly
Display name of field (used for printing).
-
#format ⇒ Object
readonly
Format for printed value of field.
-
#length ⇒ Object
(also: #size)
readonly
Length of field in bits.
-
#name ⇒ Object
readonly
Name of field (used for its accessors).
-
#offset ⇒ Object
readonly
Offset of field in bits.
-
#options ⇒ Object
readonly
Options, such as :default (varies for each field subclass).
Class Method Summary collapse
-
.class_name ⇒ Object
Used in describe.
-
.default ⇒ Object
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.
Instance Method Summary collapse
-
#class_name ⇒ Object
Used in describe.
-
#describe(opts) {|["@%d" % byte_offset, class_name, name, len_str, display_name]| ... } ⇒ Object
Yield the description of this field, as an array of 5 strings: byte offset, type, name, size, and description.
-
#initialize(offset, length, name, opts = {}) ⇒ Field
constructor
Options are display_name, default, and format (subclasses of Field may add other options).
-
#inspect_in_object(obj, opts) ⇒ Object
Inspect the value of this field in the specified obj.
-
#inspectable? ⇒ Boolean
Normally, all fields show up in inspect, but some, such as padding, should not.
Constructor Details
#initialize(offset, length, name, opts = {}) ⇒ Field
Options are display_name, default, and format (subclasses of Field may add other options).
78 79 80 81 82 83 84 85 |
# File 'lib/bit-struct/bit-struct.rb', line 78 def initialize(offset, length, name, opts = {}) @offset, @length, @name, @options = offset, length, name, opts @display_name = opts[:display_name] || opts["display_name"] @default = opts[:default] || opts["default"] || self.class.default @format = opts[:format] || opts["format"] end |
Instance Attribute Details
#default ⇒ Object (readonly)
Default value.
33 34 35 |
# File 'lib/bit-struct/bit-struct.rb', line 33 def default @default end |
#display_name ⇒ Object (readonly)
Display name of field (used for printing).
30 31 32 |
# File 'lib/bit-struct/bit-struct.rb', line 30 def display_name @display_name end |
#format ⇒ Object (readonly)
Format for printed value of field.
36 37 38 |
# File 'lib/bit-struct/bit-struct.rb', line 36 def format @format end |
#length ⇒ Object (readonly) Also known as: size
Length of field in bits.
19 20 21 |
# File 'lib/bit-struct/bit-struct.rb', line 19 def length @length end |
#name ⇒ Object (readonly)
Name of field (used for its accessors).
23 24 25 |
# File 'lib/bit-struct/bit-struct.rb', line 23 def name @name end |
#offset ⇒ Object (readonly)
Offset of field in bits.
16 17 18 |
# File 'lib/bit-struct/bit-struct.rb', line 16 def offset @offset end |
#options ⇒ Object (readonly)
Options, such as :default (varies for each field subclass). In general, options can be provided as strings or as symbols.
27 28 29 |
# File 'lib/bit-struct/bit-struct.rb', line 27 def @options end |
Class Method Details
.class_name ⇒ Object
Used in describe.
47 48 49 |
# File 'lib/bit-struct/bit-struct.rb', line 47 def self.class_name @class_name ||= name[/\w+$/] end |
.default ⇒ Object
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.
44 |
# File 'lib/bit-struct/bit-struct.rb', line 44 def self.default; nil; end |
Instance Method Details
#class_name ⇒ Object
Used in describe. Can be overridden per-subclass, as in NestedField.
52 53 54 |
# File 'lib/bit-struct/bit-struct.rb', line 52 def class_name self.class.class_name end |
#describe(opts) {|["@%d" % byte_offset, class_name, name, len_str, display_name]| ... } ⇒ Object
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.)
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bit-struct/bit-struct.rb', line 63 def describe opts bits = size if bits > 32 and bits % 8 == 0 len_str = "%dB" % (bits/8) else len_str = "%db" % bits end byte_offset = offset / 8 + (opts[:byte_offset] || 0) yield ["@%d" % byte_offset, class_name, name, len_str, display_name] end |
#inspect_in_object(obj, opts) ⇒ Object
Inspect the value of this field in the specified obj.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bit-struct/bit-struct.rb', line 88 def inspect_in_object(obj, opts) val = obj.send(name) str = begin val.inspect(opts) rescue ArgumentError # assume: "wrong number of arguments (1 for 0)" val.inspect end (f=@format) ? (f % str) : str end |
#inspectable? ⇒ Boolean
Normally, all fields show up in inspect, but some, such as padding, should not.
101 |
# File 'lib/bit-struct/bit-struct.rb', line 101 def inspectable?; true; end |