module StructPacking::Packable
Packable
module provides object-packing function.
A class include this module, and call struct defining method, pack method returns defined byte-array-form of that class's instance.
SYNOPSIS:¶ ↑
class PackableOStruct < OpenStruct include StructPacking::Packable self.byte_format = "int foo; char bar; byte[1] baz;" end obj = PackableOStruct.new obj.foo = 1 obj.bar = 2 obj.baz = [8] packed_bytes = obj.pack # => "\x01\x00\x00\x00\x02\b"
Private Class Methods
included(base)
click to toggle source
# File lib/struct_packing/packable.rb, line 25 def self.included(base) base.send(:include, Base) end
Public Instance Methods
pack()
click to toggle source
Pack this object to byte array.
If attribute defined in byte_format, but object has no attr_getter, treat as the attribute is zero.
# File lib/struct_packing/packable.rb, line 62 def pack() values = struct_values values.flatten! values.pack( pack_template ) end
Protected Instance Methods
struct_values()
click to toggle source
# File lib/struct_packing/packable.rb, line 39 def struct_values internal_format.collect do |name, type| if type =~ /^struct\s*\w*\s*(?:\s*\[(\d+)\s*\])?\s*$/ arynum = $1 if $1 == nil value_or_zero { send(:selfclass).get_field_value(self, name).struct_values } else (0..(arynum.to_i-1)).each.collect do |i| value_or_zero { send(:selfclass).get_field_value(self, name)[i].struct_values } end end else value_or_zero { send(:selfclass).get_field_value(self, name) } end end end
Private Instance Methods
value_or_zero(&block)
click to toggle source
# File lib/struct_packing/packable.rb, line 29 def value_or_zero(&block) begin instance_eval &block rescue 0 end end