Class: Csvbuilder::Import::Csv

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/csvbuilder/importer/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

Returns a new instance of Csv.



18
19
20
21
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 18

def initialize(file_path)
  @file_path = file_path
  reset
end

Instance Attribute Details

#current_rowArray? (readonly)

Returns the current row, or nil at the beginning or end of file.

Returns:

  • (Array, nil)

    the current row, or nil at the beginning or end of file



12
13
14
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 12

def current_row
  @current_row
end

#file_pathString (readonly)

Returns the file path of the CSV.

Returns:

  • (String)

    the file path of the CSV



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

def file_path
  @file_path
end

#line_numberInteger? (readonly)

Return ‘0` at start of file, `1 to infinity` is line_number of row_model, `nil` is end of file (row is also `nil`)

Returns:

  • (Integer, nil)

    return ‘0` at start of file, `1 to infinity` is line_number of row_model, `nil` is end of file (row is also `nil`)



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

def line_number
  @line_number
end

Instance Method Details

#end_of_file?Boolean

Returns true, if the current position is at the end of the file.

Returns:

  • (Boolean)

    true, if the current position is at the end of the file



60
61
62
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 60

def end_of_file?
  line_number.nil?
end

#headersArray?

Returns the header __without__ changing the position of the CSV

Returns:

  • (Array, nil)

    the header



37
38
39
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 37

def headers
  @headers ||= next_row
end

#next_rowArray?

Returns the next row __without__ changing the position of the CSV

Returns:

  • (Array, nil)

    the next row, or ‘nil` at the end of file



66
67
68
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 66

def next_row
  @next_row ||= _read_row
end

#read_rowArray?

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

Returns:

  • (Array, nil)

    the changed current row, or ‘nil` at the end of file



72
73
74
75
76
77
78
79
80
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 72

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



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 42

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

Returns:

  • (Integer)

    the number of rows in the file, including empty new lines



25
26
27
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 25

def size
  @size ||= ::File.readlines(file_path).length
end

#skip_headersBoolean, Array

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

Returns:

  • (Boolean, Array)

    returns false, if header is already skipped, otherwise returns the header



31
32
33
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 31

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

#start_of_file?Boolean

Returns true, if the current position is at the start of the file.

Returns:

  • (Boolean)

    true, if the current position is at the start of the file



55
56
57
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 55

def start_of_file?
  line_number.zero?
end