Class: CSVUtils::CSVIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/csv_utils/csv_iterator.rb

Overview

Search a CSV given a series of steps

Defined Under Namespace

Classes: RowWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_csv, csv_options = {}) ⇒ CSVIterator

Returns a new instance of CSVIterator.



24
25
26
# File 'lib/csv_utils/csv_iterator.rb', line 24

def initialize(src_csv, csv_options = {})
  @src_csv = CSVUtils::CSVWrapper.new(src_csv, 'rb', csv_options)
end

Instance Attribute Details

#prev_rowObject (readonly)

Returns the value of attribute prev_row.



5
6
7
# File 'lib/csv_utils/csv_iterator.rb', line 5

def prev_row
  @prev_row
end

Instance Method Details

#each(headers = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/csv_utils/csv_iterator.rb', line 28

def each(headers = nil)
  @src_csv.rewind

  lineno = 0
  unless headers
    headers = @src_csv.shift
    strip_bom!(headers[0])
    lineno += 1
  end

  @prev_row = nil
  while (row = @src_csv.shift)
    lineno += 1
    yield RowWrapper.create(headers, row, lineno)
    @prev_row = row
  end
end