Class: CSV::Parser

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

Defined Under Namespace

Classes: InputsScanner, InvalidEncoding, Scanner, UnoptimizedStringIO

Instance Method Summary collapse

Constructor Details

#initialize(input, options) ⇒ Parser

Returns a new instance of Parser.



191
192
193
194
195
196
197
# File 'lib/csv/parser.rb', line 191

def initialize(input, options)
  @input = input
  @options = options
  @samples = []

  prepare
end

Instance Method Details

#column_separatorObject



199
200
201
# File 'lib/csv/parser.rb', line 199

def column_separator
  @column_separator
end

#field_size_limitObject



211
212
213
# File 'lib/csv/parser.rb', line 211

def field_size_limit
  @field_size_limit
end

#header_row?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/csv/parser.rb', line 227

def header_row?
  @use_headers and @headers.nil?
end

#headersObject



223
224
225
# File 'lib/csv/parser.rb', line 223

def headers
  @headers
end

#liberal_parsing?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/csv/parser.rb', line 239

def liberal_parsing?
  @liberal_parsing
end

#lineObject



247
248
249
# File 'lib/csv/parser.rb', line 247

def line
  last_line
end

#linenoObject



243
244
245
# File 'lib/csv/parser.rb', line 243

def lineno
  @lineno
end

#parse(&block) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/csv/parser.rb', line 251

def parse(&block)
  return to_enum(__method__) unless block_given?

  if @return_headers and @headers and @raw_headers
    headers = Row.new(@headers, @raw_headers, true)
    if @unconverted_fields
      headers = add_unconverted_fields(headers, [])
    end
    yield headers
  end

  begin
    @scanner ||= build_scanner
    if quote_character.nil?
      parse_no_quote(&block)
    else
      parse_quotable(&block)
    end
  rescue InvalidEncoding
    if @scanner
      ignore_broken_line
      lineno = @lineno
    else
      lineno = @lineno + 1
    end
    message = "Invalid byte sequence in #{@encoding}"
    raise MalformedCSVError.new(message, lineno)
  end
end

#quote_characterObject



207
208
209
# File 'lib/csv/parser.rb', line 207

def quote_character
  @quote_character
end

#return_headers?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/csv/parser.rb', line 231

def return_headers?
  @return_headers
end

#row_separatorObject



203
204
205
# File 'lib/csv/parser.rb', line 203

def row_separator
  @row_separator
end

#skip_blanks?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/csv/parser.rb', line 235

def skip_blanks?
  @skip_blanks
end

#skip_linesObject



215
216
217
# File 'lib/csv/parser.rb', line 215

def skip_lines
  @skip_lines
end

#unconverted_fields?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/csv/parser.rb', line 219

def unconverted_fields?
  @unconverted_fields
end

#use_headers?Boolean

Returns:

  • (Boolean)


281
282
283
# File 'lib/csv/parser.rb', line 281

def use_headers?
  @use_headers
end