class Bytemapper::Chunk

Attributes

bytes[R]
shape[R]

Public Class Methods

new(bytes, shape, name) click to toggle source
# File lib/bytemapper/chunk.rb, line 24
def initialize(bytes, shape, name)
  @name = name
  @shape = shape
  @bytes = bytes.is_a?(StringIO) ? bytes : StringIO.new(bytes)
  replace(shape)
  each_pair do |k,v|
    self[k] = if v.is_a?(Hash)
                Chunk.new(@bytes.read(v.size), v, k)
              elsif v.is_a?(Bytemapper::Table)
                if v.empty?
                  v.populate(@bytes.read(@bytes.size-@bytes.pos))
                else
                  v.populate(@bytes.read(v.capacity))
                end
              else
                unpack(v)
              end
    singleton_class.instance_eval { attr_reader k }
    instance_variable_set("@#{k.to_s}", self[k])
  end

  shape.hooks.each {|h| h.call(self) }
end

Public Instance Methods

capacity() click to toggle source
# File lib/bytemapper/chunk.rb, line 60
def capacity
  shape.size
end
chr() click to toggle source
# File lib/bytemapper/chunk.rb, line 56
def chr
  bytes.string.split(//).map(&:chr)
end
consumed() click to toggle source
# File lib/bytemapper/chunk.rb, line 68
def consumed
  size
end
ord() click to toggle source
# File lib/bytemapper/chunk.rb, line 52
def ord
  bytes.string.split(//).map(&:ord)
end
remaining() click to toggle source
# File lib/bytemapper/chunk.rb, line 72
def remaining
  capacity - consumed
end
size() click to toggle source
# File lib/bytemapper/chunk.rb, line 64
def size
  bytes.size
end
string() click to toggle source
# File lib/bytemapper/chunk.rb, line 48
def string
  bytes.string
end
unpack(value, endian = nil) click to toggle source
# File lib/bytemapper/chunk.rb, line 76
def unpack(value, endian = nil)
  num_bytes, flag = value
  _bytes = bytes.read(num_bytes >> 3)
  _bytes.unpack("#{flag}#{endian}")[0] unless _bytes.nil?
end