Class: BioTable::Count::CountTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-table/count.rb

Overview

Track rows that have the same column items. Return the last match of the cummalative list with the count attached.

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ CountTracker

Returns a new instance of CountTracker.



6
7
8
9
# File 'lib/bio-table/count.rb', line 6

def initialize list
  @list = list.map { |item| item.to_i }
  @rows = []
end

Instance Method Details

#add(row, type, flush: false) ⇒ Object

Add a row and if it differs send the last merged edition back type is :header or :row



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bio-table/count.rb', line 13

def add row, type, flush: false
  return row+["count"] if type == :header
  num = @rows.size
  prev = @rows.last
  if flush
    prev+[num]
  else
    # Take the list and compare each item to the previous row
    prev_same = if prev
                  @list.reduce(true) { |memo,i| memo && (row[i]==prev[i]) }
                else
                  false
                end
    if prev_same
      @rows << row
    else
      @rows = []
      @rows << row
      return prev+[num] if prev
    end
    nil
  end
end