class CSVLibrary
Using relevant core CSV
library.
Public Class Methods
fastercsv?()
click to toggle source
Is the library we’re using FasterCSV?
# 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)
click to toggle source
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
# File lib/ndr_import/csv_library.rb, line 23 def foreach(path, **options, &block) deprecate('CSV#foreach exists, with an optional `mode` argument') return to_enum(__method__, path, **options) unless block open(path, options.delete(:mode) || 'r', **options) do |csv| csv.each(&block) end end
read_csv_from_file(filepath)
click to toggle source
# 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')
click to toggle source
# 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)
click to toggle source
# 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
Private Class Methods
deprecate(additional_message = nil)
click to toggle source
# File lib/ndr_import/csv_library.rb, line 52 def deprecate(additional_message = nil) ActiveSupport::Deprecation.warn(<<~MESSAGE) CSVLibrary is deprecated, and will be removed in a future version of ndr_import. Please use standard functionality provided by Ruby's CSV library (#{additional_message}). MESSAGE end