Class: BitStruct::OctetField
- Defined in:
- lib/bit-struct/octet-field.rb
Overview
Class for char fields that can be accessed with values like “xxx.xxx.xxx.xxx”, where each xxx is up to 3 decimal digits representing a single octet. The original string-based accessors are still available with the _chars
suffix.
Declared with BitStruct.octets.
Direct Known Subclasses
Constant Summary collapse
- SEPARATOR =
"."
- FORMAT =
"%d"
- BASE =
10
Instance Attribute Summary
Attributes inherited from Field
#default, #display_name, #format, #length, #name, #offset, #options
Class Method Summary collapse
-
.class_name ⇒ Object
Used in describe.
Instance Method Summary collapse
Methods inherited from Field
#class_name, default, #describe, #initialize, #inspect_in_object, #inspectable?
Constructor Details
This class inherits a constructor from BitStruct::Field
Class Method Details
.class_name ⇒ Object
Used in describe.
12 13 14 |
# File 'lib/bit-struct/octet-field.rb', line 12 def self.class_name @class_name ||= "octets" end |
Instance Method Details
#add_accessors_to(cl, attr = name) ⇒ Object
:nodoc:
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bit-struct/octet-field.rb', line 20 def add_accessors_to(cl, attr = name) # :nodoc: attr_chars = "#{attr}_chars" super(cl, attr_chars) sep = self.class::SEPARATOR base = self.class::BASE fmt = self.class::FORMAT cl.class_eval do define_method attr do || ary = [] send(attr_chars).each_byte do |c| ary << fmt % c end ary.join(sep) end old_writer = "#{attr_chars}=" define_method "#{attr}=" do |val| data = val.split(sep).map{|s|s.to_i(base)}.pack("c*") send(old_writer, data) end end end |