Class: CsvRowModel::Import::Csv

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/csv_row_model/internal/import/csv.rb

Overview

Abstraction of Ruby's CSV library. Keeps current row and line_number, skips empty rows, handles errors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Csv


16
17
18
19
# File 'lib/csv_row_model/internal/import/csv.rb', line 16

def initialize(file_path)
  @file_path = file_path
  reset
end

Instance Attribute Details

#current_rowArray? (readonly)


10
11
12
# File 'lib/csv_row_model/internal/import/csv.rb', line 10

def current_row
  @current_row
end

#file_pathString (readonly)


6
7
8
# File 'lib/csv_row_model/internal/import/csv.rb', line 6

def file_path
  @file_path
end

#line_numberInteger? (readonly)


8
9
10
# File 'lib/csv_row_model/internal/import/csv.rb', line 8

def line_number
  @line_number
end

Instance Method Details

#_read_rowObject (protected)


84
85
86
87
88
89
90
91
# File 'lib/csv_row_model/internal/import/csv.rb', line 84

def _read_row
  return unless valid?
  @ruby_csv.readline.tap { |row| @headers ||= row }
rescue Exception => e
  changed = e.exception(e.message.gsub(/line \d+\.?/, "line #{line_number + 1}.")) # line numbers are usually off
  changed.set_backtrace(e.backtrace)
  changed
end

#_ruby_csvObject (protected)


80
81
82
# File 'lib/csv_row_model/internal/import/csv.rb', line 80

def _ruby_csv
  CSV.open(file_path)
end

#end_of_file?Boolean


57
58
59
# File 'lib/csv_row_model/internal/import/csv.rb', line 57

def end_of_file?
  line_number.nil?
end

#headersArray?

Returns the header without changing the position of the CSV


35
36
37
# File 'lib/csv_row_model/internal/import/csv.rb', line 35

def headers
  @headers ||= next_row
end

#next_rowArray?

Returns the next row without changing the position of the CSV


63
64
65
# File 'lib/csv_row_model/internal/import/csv.rb', line 63

def next_row
  @next_row ||= _read_row
end

#read_rowArray?

Returns the next row, while changing the position of the CSV


69
70
71
72
73
74
75
76
77
# File 'lib/csv_row_model/internal/import/csv.rb', line 69

def read_row
  return if end_of_file?

  @current_row = @next_row || _read_row
  @line_number = current_row.nil? ? nil : @line_number + 1
  @next_row = nil

  current_row
end

#resetObject

Resets the file to the start of file


40
41
42
43
44
45
46
47
48
49
# File 'lib/csv_row_model/internal/import/csv.rb', line 40

def reset
  return false unless valid?

  @line_number = 0
  @headers = @current_row = @next_row = @skipped_rows = @next_skipped_rows = nil

  @ruby_csv.try(:close)
  @ruby_csv = _ruby_csv
  true
end

#sizeInteger


23
24
25
# File 'lib/csv_row_model/internal/import/csv.rb', line 23

def size
  @size ||= `wc -l #{file_path}`.split[0].to_i + 1
end

#skip_headersBoolean, Array

If the current position is at the headers, skip it and return it. Otherwise, only return false.


29
30
31
# File 'lib/csv_row_model/internal/import/csv.rb', line 29

def skip_headers
  start_of_file? ? (@headers = read_row) : false
end

#start_of_file?Boolean


52
53
54
# File 'lib/csv_row_model/internal/import/csv.rb', line 52

def start_of_file?
  line_number == 0
end