Class: Zippo::BinaryStructure::BinaryUnpacker

Inherits:
Object
  • Object
show all
Defined in:
lib/zippo/binary_structure/binary_unpacker.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ BinaryUnpacker

Returns a new instance of BinaryUnpacker.



10
11
12
13
# File 'lib/zippo/binary_structure/binary_unpacker.rb', line 10

def initialize(io)
  @io = io
  @io = StringIO.new @io if @io.is_a? String
end

Class Attribute Details

.structureObject

Returns the value of attribute structure.



7
8
9
# File 'lib/zippo/binary_structure/binary_unpacker.rb', line 7

def structure
  @structure
end

Instance Method Details

#unpackObject

default implementation note that this will generally be overridden by define_unpack_method for optimisation



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/zippo/binary_structure/binary_unpacker.rb', line 18

def unpack
  self.class.structure.owner_class.new.tap do |obj|
    self.class.structure.fields.each do |field|
      if field.options[:size]
        obj.instance_variable_set "@#{field.name}", @io.read(obj.send field.options[:size])
      else
        buf = @io.read field.width
        obj.instance_variable_set "@#{field.name}", buf.unpack(field.pack).first
      end
    end
  end
end