Class: Bytemapper::Table

Inherits:
Array
  • Object
show all
Includes:
Flattenable, Nameable
Defined in:
lib/bytemapper/table.rb

Instance Attribute Summary collapse

Attributes included from Nameable

#name, #names

Instance Method Summary collapse

Methods included from Flattenable

#flatten

Constructor Details

#initialize(shape, rows = nil) ⇒ Table

Returns a new instance of Table.



16
17
18
19
# File 'lib/bytemapper/table.rb', line 16

def initialize(shape, rows = nil)
  @shape = Bytemapper.get(shape)
  rows.times { self << @shape  }
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



13
14
15
# File 'lib/bytemapper/table.rb', line 13

def bytes
  @bytes
end

#rowsObject (readonly)

Returns the value of attribute rows.



13
14
15
# File 'lib/bytemapper/table.rb', line 13

def rows
  @rows
end

#shapeObject (readonly)

Returns the value of attribute shape.



13
14
15
# File 'lib/bytemapper/table.rb', line 13

def shape
  @shape
end

Instance Method Details

#populate(bytes) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bytemapper/table.rb', line 21

def populate(bytes)
  bytes = bytes.nil? ? '' : bytes
  @bytes = bytes.is_a?(StringIO) ? bytes : StringIO.new(bytes)
  @bytes.string.force_encoding(Encoding::ASCII_8BIT)
  if unbounded?
    (bytes.size / shape.size).times { self << @shape } 
  end

  table = Table.new(shape) 
  table.clear
  (bytes.size / shape.size).times { table << Chunk.new(@bytes.read(shape.size), shape, shape.name) }
  table
end

#sizeObject



39
40
41
# File 'lib/bytemapper/table.rb', line 39

def size
  empty? ? 0 : map(&:size).reduce(:+)
end

#unbounded?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/bytemapper/table.rb', line 35

def unbounded?
  empty?
end