Class: CSVLibrary
- Inherits:
-
CSV
- Object
- CSV
- CSVLibrary
- Defined in:
- lib/ndr_import/csv_library.rb
Overview
Using relevant core CSV library.
Class Method Summary collapse
-
.fastercsv? ⇒ Boolean
Is the library we’re using FasterCSV?.
-
.foreach(path, **options, &block) ⇒ Object
Ensure that we can pass “mode” straight through the underlying IO object.
- .read_csv_from_file(filepath) ⇒ Object
- .write_csv_to_file(data, filepath, mode = 'w') ⇒ Object
- .write_csv_to_string(data) ⇒ Object
Class Method Details
.fastercsv? ⇒ Boolean
Is the library we’re using FasterCSV?
11 12 13 14 |
# File 'lib/ndr_import/csv_library.rb', line 11 def fastercsv? deprecate('if you desparately want fastercsv, please use it explicitly') not self.const_defined?(:Reader) end |
.foreach(path, **options, &block) ⇒ Object
Ensure that we can pass “mode” straight through the underlying IO object
Note: this could likely be refactored now, as upstream support for something
very similar was added:
https://github.com/ruby/csv/commit/b4edaf2cf1aa36f5c6264c07514b66739b87ceee
23 24 25 26 27 28 29 |
# File 'lib/ndr_import/csv_library.rb', line 23 def foreach(path, **, &block) deprecate('CSV#foreach exists, with an optional `mode` argument') return to_enum(__method__, path, **) unless block open(path, .delete(:mode) || 'r', **) do |csv| csv.each(&block) end end |
.read_csv_from_file(filepath) ⇒ Object
45 46 47 48 |
# File 'lib/ndr_import/csv_library.rb', line 45 def read_csv_from_file(filepath) deprecate('read_csv_from_file -> read') self.read(filepath) end |
.write_csv_to_file(data, filepath, mode = 'w') ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ndr_import/csv_library.rb', line 38 def write_csv_to_file(data, filepath, mode = 'w') deprecate('write_csv_to_file -> open') self.open(filepath, mode) do |csv| data.each { |line| csv << line } end end |
.write_csv_to_string(data) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/ndr_import/csv_library.rb', line 31 def write_csv_to_string(data) deprecate('write_csv_to_string -> generate') self.generate do |csv| data.each { |line| csv << line } end end |