Class: CSVUtils::CSVWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_utils/csv_wrapper.rb

Overview

Wraps a path or an open CSV so callers can read, write and close it the same way. It closes only the files it opened itself.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv, mode, csv_options) ⇒ CSVWrapper

Returns a new instance of CSVWrapper.

Parameters:

  • csv (String, CSV, #shift, #<<) —

    path to open, or a CSV (or array of rows) to use as is

  • mode (String) —

    file mode for a path

  • csv_options (Hash) —

    options passed to CSV.open for a path



13
14
15
# File 'lib/csv_utils/csv_wrapper.rb', line 13

def initialize(csv, mode, csv_options)
  open(csv, mode, csv_options)
end

Instance Attribute Details

#csv ⇒ CSV, ... (readonly)

Returns the CSV being wrapped.

Returns:

  • (CSV, #shift, #<<) —

    the CSV being wrapped



8
9
10
# File 'lib/csv_utils/csv_wrapper.rb', line 8

def csv
  @csv
end

Class Method Details

.open(file, mode, csv_options = {}) {|csv| ... } ⇒ CSVWrapper, Object

Wraps a path or CSV; with a block, yields the wrapper and closes it afterwards, even when the block raises.

Parameters:

  • file (String, CSV) —

    path or CSV

  • mode (String) —

    file mode for a path

  • csv_options (Hash) (defaults to: {}) —

    options passed to CSV.open for a path

Yield Parameters:

Returns:

  • (CSVWrapper, Object) —

    the wrapper, or the block's value



23
24
25
26
27
28
29
30
31
32
# File 'lib/csv_utils/csv_wrapper.rb', line 23

def self.open(file, mode, csv_options = {})
  csv = new(file, mode, csv_options)
  return csv unless block_given?

  begin
    yield csv
  ensure
    csv.close
  end
end

Instance Method Details

#<<(row) ⇒ CSV, #<<

Parameters:

  • row (Array)

Returns:

  • (CSV, #<<)


48
49
50
# File 'lib/csv_utils/csv_wrapper.rb', line 48

def <<(row)
  csv << row
end

#close ⇒ void

This method returns an undefined value.

Closes the CSV if the wrapper opened it.



75
76
77
# File 'lib/csv_utils/csv_wrapper.rb', line 75

def close
  csv.close if close_when_done?
end

#line ⇒ String?

Returns the raw text of the last row read, with its row separator.

Returns:

  • (String, nil) —

    the raw text of the last row read, with its row separator



58
59
60
# File 'lib/csv_utils/csv_wrapper.rb', line 58

def line
  csv.line
end

#open(csv, mode, csv_options) ⇒ CSV, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



36
37
38
39
40
41
42
43
44
# File 'lib/csv_utils/csv_wrapper.rb', line 36

def open(csv, mode, csv_options)
  if csv.is_a?(String)
    @close_when_done = true
    @csv = CSV.open(csv, mode, **csv_options)
  else
    @close_when_done = false
    @csv = csv
  end
end

#rewind ⇒ void

This method returns an undefined value.

Moves back to the first row.



69
70
71
# File 'lib/csv_utils/csv_wrapper.rb', line 69

def rewind
  csv.rewind
end

#row_sep ⇒ String

Returns the row separator, detected from the file when it was :auto.

Returns:

  • (String) —

    the row separator, detected from the file when it was :auto



63
64
65
# File 'lib/csv_utils/csv_wrapper.rb', line 63

def row_sep
  csv.row_sep
end

#shift ⇒ Array<String>?

Returns the next row, nil at the end.

Returns:

  • (Array<String>, nil) —

    the next row, nil at the end



53
54
55
# File 'lib/csv_utils/csv_wrapper.rb', line 53

def shift
  csv.shift
end