Class: CSV::Parser

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

Overview

Note: Don’t use this class directly. This is an internal class.

Defined Under Namespace

Classes: InputsScanner, InvalidEncoding, Scanner, UnexpectedError, UnoptimizedStringIO

Instance Method Summary collapse

Constructor Details

#initialize(input, options) ⇒ Parser

Returns a new instance of Parser.



324
325
326
327
328
329
330
# File 'lib/csv/parser.rb', line 324

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

  prepare
end

Instance Method Details

#column_separatorObject



332
333
334
# File 'lib/csv/parser.rb', line 332

def column_separator
  @column_separator
end

#field_size_limitObject



344
345
346
# File 'lib/csv/parser.rb', line 344

def field_size_limit
  @max_field_size&.succ
end

#header_row?Boolean

Returns:

  • (Boolean)


364
365
366
# File 'lib/csv/parser.rb', line 364

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

#headersObject



360
361
362
# File 'lib/csv/parser.rb', line 360

def headers
  @headers
end

#liberal_parsing?Boolean

Returns:

  • (Boolean)


376
377
378
# File 'lib/csv/parser.rb', line 376

def liberal_parsing?
  @liberal_parsing
end

#lineObject



384
385
386
# File 'lib/csv/parser.rb', line 384

def line
  last_line
end

#linenoObject



380
381
382
# File 'lib/csv/parser.rb', line 380

def lineno
  @lineno
end

#max_field_sizeObject



348
349
350
# File 'lib/csv/parser.rb', line 348

def max_field_size
  @max_field_size
end

#parse(&block) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/csv/parser.rb', line 388

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)
    elsif @need_robust_parsing
      parse_quotable_robust(&block)
    else
      parse_quotable_loose(&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)
  rescue UnexpectedError => error
    if @scanner
      ignore_broken_line
      lineno = @lineno
    else
      lineno = @lineno + 1
    end
    message = "This should not be happen: #{error.message}: "
    message += "Please report this to https://github.com/ruby/csv/issues"
    raise MalformedCSVError.new(message, lineno)
  end
end

#quote_characterObject



340
341
342
# File 'lib/csv/parser.rb', line 340

def quote_character
  @quote_character
end

#return_headers?Boolean

Returns:

  • (Boolean)


368
369
370
# File 'lib/csv/parser.rb', line 368

def return_headers?
  @return_headers
end

#row_separatorObject



336
337
338
# File 'lib/csv/parser.rb', line 336

def row_separator
  @row_separator
end

#skip_blanks?Boolean

Returns:

  • (Boolean)


372
373
374
# File 'lib/csv/parser.rb', line 372

def skip_blanks?
  @skip_blanks
end

#skip_linesObject



352
353
354
# File 'lib/csv/parser.rb', line 352

def skip_lines
  @skip_lines
end

#unconverted_fields?Boolean

Returns:

  • (Boolean)


356
357
358
# File 'lib/csv/parser.rb', line 356

def unconverted_fields?
  @unconverted_fields
end

#use_headers?Boolean

Returns:

  • (Boolean)


430
431
432
# File 'lib/csv/parser.rb', line 430

def use_headers?
  @use_headers
end