Class: RdbCSVReader::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/rdb_csv/reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(fp, db, delimiter, options) ⇒ Reader

Returns a new instance of Reader.



3
4
5
6
7
8
9
10
# File 'lib/rdb_csv/reader.rb', line 3

def initialize(fp, db, delimiter, options)
  @fp = fp
  @db = db
  @delimiter = delimiter
  @escape = options[:escape] || "\\" # 1 char
  @linefeed = options[:linefeed] || "\n"
  @quote = options[:quote] || '"'
end

Instance Method Details

#each_lineObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rdb_csv/reader.rb', line 12

def each_line
  str = ''
  row = []
  quote_num = 0

  each_char do |char|
    if quote_num > 0
      if str[-1] == @quote && char == @quote
        quote_num -= 1
      elsif str[-1] != @quote && char == @quote
        quote_num -= 1
      elsif char == @quote
        quote_num += 1
      end
      str += char
    else
      if char == @delimiter
        row << str
        str = ''
      elsif char == @linefeed
        row << str
        yield RdbCSV::CSV::Row.new(row)
        row = []
        str = ''
      else
        quote_num += 1 if (str.size == 0 || str[0] == @quote) && char == @quote
        str += char
      end
    end
  end
end