module StructPacking::Unpackable::ClassMethods

Extending methods for Unpackable class.

Automatically extend on including StructPacking::Unpackable module.

Public Instance Methods

from_data(bytes)
Alias for: unpack
unpack(bytes) click to toggle source

Construct object byte array.

This method is simply do object construct and values assignment. If attribute defined in byte_format, but object has no attr_setter, do nothing.

TODO: Including class must have default constructor.

# File lib/struct_packing/unpackable.rb, line 33
def unpack(bytes)  
  obj = self.new
  set_values_from_byte_to_object(bytes, obj)
end
Also aliased as: from_data

Protected Instance Methods

from_values(values) click to toggle source

Instantiate and initialize object by value-array.

# File lib/struct_packing/unpackable.rb, line 44
def from_values(values)
  obj = self.new
  set_values_from_values_to_object(values, obj)
end

Private Instance Methods

field_names() click to toggle source

Get field name list of this class.

# File lib/struct_packing/unpackable.rb, line 57
def field_names
  internal_format.keys
end
gather_array_field(value_array) click to toggle source
# File lib/struct_packing/unpackable.rb, line 72
def gather_array_field(value_array)
  values = value_array.dup
  
  internal_format.collect do |name, type|

    if type =~ /^struct\s*(\w*)\s*(?:\s*\[(\d+)\s*\])?\s*$/
      struct_name = $1
      arylen = $2
      cls = Util.find_hier_mod(self, struct_name)
      if arylen == nil
        obj = cls.from_values( values[0, cls.send(:num_of_value)] )
        values = values[cls.send(:num_of_value), values.length]
      else
        obj = []
        
        arylen.to_i.times do 
          obj.push( cls.from_values( values[0, cls.send(:num_of_value)] ) )
          values = values[cls.send(:num_of_value), values.length]
        end
      end
      
      obj
    elsif type =~ /.*\[\w*(\d+)\w*\]\w*/
      [0..$1.to_i].to_a.collect { values.shift }
    else
      values.shift
    end
  end
end
num_of_value() click to toggle source
# File lib/struct_packing/unpackable.rb, line 102
def num_of_value
  
  nums = internal_format.collect do |name, type|
    if type =~ /^struct\s*(\w*)\s*(?:\s*\[(\d+)\s*\])?\s*$/
      struct_name = $1
      arylen = $2
      cls = Util.find_hier_mod(self, struct_name)
      
      if arylen == nil
        cls.num_of_value
      else
        cls.num_of_value * arylen.to_i
      end
    else
      1
    end
  end
  
  nums.inject(0) do |sum, num|
    sum + num
  end
      
end
set_values_from_byte_to_object(bytes, obj) click to toggle source
# File lib/struct_packing/unpackable.rb, line 51
def set_values_from_byte_to_object(bytes, obj)
  values = bytes.unpack( pack_template )
  set_values_from_values_to_object(values, obj)
end
set_values_from_values_to_object(values, obj) click to toggle source
# File lib/struct_packing/unpackable.rb, line 61
def set_values_from_values_to_object(values, obj)
  
  field_names.zip(gather_array_field(values) ).each do |name,value|
    begin
      obj.send(:selfclass).set_field_value(obj, name, value)
    rescue NoMethodError
    end
  end
  obj
end