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.



225
226
227
228
229
230
231
# File 'lib/csv/parser.rb', line 225

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

  prepare
end

Instance Method Details

#column_separatorObject



233
234
235
# File 'lib/csv/parser.rb', line 233

def column_separator
  @column_separator
end

#field_size_limitObject



245
246
247
# File 'lib/csv/parser.rb', line 245

def field_size_limit
  @field_size_limit
end

#header_row?Boolean

Returns:

  • (Boolean)


261
262
263
# File 'lib/csv/parser.rb', line 261

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

#headersObject



257
258
259
# File 'lib/csv/parser.rb', line 257

def headers
  @headers
end

#liberal_parsing?Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/csv/parser.rb', line 273

def liberal_parsing?
  @liberal_parsing
end

#lineObject



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

def line
  last_line
end

#linenoObject



277
278
279
# File 'lib/csv/parser.rb', line 277

def lineno
  @lineno
end

#parse(&block) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/csv/parser.rb', line 285

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)
  end
end

#quote_characterObject



241
242
243
# File 'lib/csv/parser.rb', line 241

def quote_character
  @quote_character
end

#return_headers?Boolean

Returns:

  • (Boolean)


265
266
267
# File 'lib/csv/parser.rb', line 265

def return_headers?
  @return_headers
end

#row_separatorObject



237
238
239
# File 'lib/csv/parser.rb', line 237

def row_separator
  @row_separator
end

#skip_blanks?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/csv/parser.rb', line 269

def skip_blanks?
  @skip_blanks
end

#skip_linesObject



249
250
251
# File 'lib/csv/parser.rb', line 249

def skip_lines
  @skip_lines
end

#unconverted_fields?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/csv/parser.rb', line 253

def unconverted_fields?
  @unconverted_fields
end

#use_headers?Boolean

Returns:

  • (Boolean)


317
318
319
# File 'lib/csv/parser.rb', line 317

def use_headers?
  @use_headers
end