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



23
24
25
26
27
# File 'lib/bamfcsv/table.rb', line 23

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
19
20
21
# File 'lib/bamfcsv/table.rb', line 14

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

#empty?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/bamfcsv/table.rb', line 29

def empty?
  @matrix.empty?
end

#inspectObject



33
34
35
# File 'lib/bamfcsv/table.rb', line 33

def inspect
  "[#{self.map{|r| r.inspect}.join(", ")}]"
end