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

#each_batch(batch_size = 1_000) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/csv_utils/csv_iterator.rb', line 79

def each_batch(batch_size = 1_000)
  batch = []

  process_batch_proc = Proc.new do
    yield batch
    batch = []
  end

  each do |row|
    batch << row
    process_batch_proc.call if batch.size >= batch_size
  end

  process_batch_proc.call if batch.size > 0

  nil
end

#headersObject



46
47
48
49
50
51
# File 'lib/csv_utils/csv_iterator.rb', line 46

def headers
  @src_csv.rewind
  headers = @src_csv.shift
  strip_bom!(headers[0])
  headers
end

#sizeObject



69
70
71
72
73
74
75
76
77
# File 'lib/csv_utils/csv_iterator.rb', line 69

def size
  @src_csv.rewind
  @src_csv.shift
  cnt = 0
  while @src_csv.shift
    cnt +=1
  end
  cnt
end

#to_hash(key, value = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/csv_utils/csv_iterator.rb', line 53

def to_hash(key, value = nil)
  raise("header #{key} not found in #{headers}") unless headers.include?(key)
  raise("headers #{value} not found in #{headers}") if value && !headers.include?(value)

  value_proc =
    if value
      proc { |row| row[value] }
    else
      proc { |row| yield(row) }
    end

  each_with_object({}) do |row, hsh|
    hsh[row[key]] = value_proc.call(row)
  end
end