Class: CSVUtils::CSVWrapper
- Inherits:
-
Object
- Object
- CSVUtils::CSVWrapper
- 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
-
#csv ⇒ CSV, ...
readonly
The CSV being wrapped.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #<<(row) ⇒ CSV, #<<
-
#close ⇒ void
Closes the CSV if the wrapper opened it.
-
#initialize(csv, mode, csv_options) ⇒ CSVWrapper
constructor
A new instance of CSVWrapper.
-
#line ⇒ String?
The raw text of the last row read, with its row separator.
- #open(csv, mode, csv_options) ⇒ CSV, ... private
-
#rewind ⇒ void
Moves back to the first row.
-
#row_sep ⇒ String
The row separator, detected from the file when it was :auto.
-
#shift ⇒ Array<String>?
The next row, nil at the end.
Constructor Details
#initialize(csv, mode, csv_options) ⇒ CSVWrapper
Returns a new instance of CSVWrapper.
13 14 15 |
# File 'lib/csv_utils/csv_wrapper.rb', line 13 def initialize(csv, mode, ) open(csv, mode, ) end |
Instance Attribute Details
#csv ⇒ CSV, ... (readonly)
Returns 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.
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 = new(file, mode, ) return csv unless block_given? begin yield csv ensure csv.close end end |
Instance Method Details
#<<(row) ⇒ 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.
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.
36 37 38 39 40 41 42 43 44 |
# File 'lib/csv_utils/csv_wrapper.rb', line 36 def open(csv, mode, ) if csv.is_a?(String) @close_when_done = true @csv = CSV.open(csv, mode, **) 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.
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.
53 54 55 |
# File 'lib/csv_utils/csv_wrapper.rb', line 53 def shift csv.shift end |