Class: BAMFCSV::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bamfcsv/table.rb

Defined Under Namespace

Classes: Row

Instance Method Summary collapse

Constructor Details

#initialize(matrix) ⇒ Table

Returns a new instance of Table.



4
5
6
7
8
9
10
11
12
# File 'lib/bamfcsv/table.rb', line 4

def initialize(matrix)
  @headers = matrix.shift
  @matrix = matrix
  @header_map = {}
  @headers.each_with_index do |h, i|
    @header_map[h] = i
  end
  @row_cache = []
end

Instance Method Details

#[](idx) ⇒ Object



20
21
22
23
24
# File 'lib/bamfcsv/table.rb', line 20

def [](idx)
  idx += @matrix.size if idx < 0
  return if idx < 0 || idx >= @matrix.size
  @row_cache[idx] ||= Row.new(@header_map, @matrix[idx])
end

#eachObject



14
15
16
17
18
# File 'lib/bamfcsv/table.rb', line 14

def each
  @matrix.size.times do |idx|
    yield self[idx]
  end
end

#inspectObject



26
27
28
# File 'lib/bamfcsv/table.rb', line 26

def inspect
  "#<BAMFCSV::Table>"
end