Module: Rstruct::ContainerMixins

Included in:
ArrayContainer
Defined in:
lib/rstruct/base_types/container_type.rb

Instance Method Summary collapse

Instance Method Details

#rstruct_typeObject



6
7
8
# File 'lib/rstruct/base_types/container_type.rb', line 6

def rstruct_type
  @rstruct_type
end

#rstruct_type=(val) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rstruct/base_types/container_type.rb', line 10

def rstruct_type=(val)
  if @rstruct_type
    raise(ArgumentError, "Can't override the rstruct_type once it is set")
  else
    @rstruct_type = val
  end
end

#write(dst = nil, pvals = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rstruct/base_types/container_type.rb', line 18

def write(dst=nil, pvals=nil)
  if dst.is_a?(String)
    l = dst.size
    dst = StringIO.new(dst)
    dst.pos = l
  elsif dst.nil?
    dst = StringIO.new
  end

  typ ||= self.rstruct_type


  vals = (pvals.respond_to?(:values) ? pvals.values : pvals)
  vals ||= self.values

  opos = dst.pos if dst.respond_to?(:pos)
  typ.fields.each_with_index do |f, i|
    fldval = vals[i]
    if fldval.respond_to?(:write)
      fldval.write(dst, fldval)
    else
      dst.write(f.typ.pack_value((fldval || 0), self))
    end
  end
  if dst.is_a?(StringIO) and pvals.nil?
    dst.pos = opos
    return(dst.read)
  elsif opos and dst.respond_to?(:pos)
    return dst.pos - opos
  end
end