Class: CSV::Parser::Scanner

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/csv/parser.rb

Overview

CSV::Scanner receives a CSV output, scans it and return the content. It also controls the life cycle of the object with its methods keep_start, keep_end, keep_back, keep_drop.

Uses StringScanner (the official strscan gem). Strscan provides lexical scanning operations on a String. We inherit its object and take advantage on the methods. For more information, please visit: docs.ruby-lang.org/en/master/StringScanner.html

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Scanner

Returns a new instance of Scanner.



55
56
57
58
# File 'lib/csv/parser.rb', line 55

def initialize(*args)
  super
  @keeps = []
end

Instance Method Details

#each_line(row_separator) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/csv/parser.rb', line 60

def each_line(row_separator)
  position = pos
  rest.each_line(row_separator) do |line|
    position += line.bytesize
    self.pos = position
    yield(line)
  end
end

#keep_backObject



78
79
80
# File 'lib/csv/parser.rb', line 78

def keep_back
  self.pos = @keeps.pop
end

#keep_dropObject



82
83
84
# File 'lib/csv/parser.rb', line 82

def keep_drop
  @keeps.pop
end

#keep_endObject



73
74
75
76
# File 'lib/csv/parser.rb', line 73

def keep_end
  start = @keeps.pop
  string.byteslice(start, pos - start)
end

#keep_startObject



69
70
71
# File 'lib/csv/parser.rb', line 69

def keep_start
  @keeps.push(pos)
end