Sha256: 8cbb592be84af6313b908c2ef6b30d932d8e7a60805eaed4086a2d6c42d0ba7f

Contents?: true

Size: 1.88 KB

Versions: 13

Compression:

Stored size: 1.88 KB

Contents

# This file allows us to choose the CSV library we want to use.

require 'csv'
require 'active_support/deprecation'

# Using relevant core CSV library.
class CSVLibrary < CSV; end

class << CSVLibrary
  # Is the library we're using FasterCSV?
  def fastercsv?
    deprecate('if you desparately want fastercsv, please use it explicitly')
    not self.const_defined?(:Reader)
  end

  # 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
  #
  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

  def write_csv_to_string(data)
    deprecate('write_csv_to_string -> generate')
    self.generate do |csv|
      data.each { |line| csv << line }
    end
  end

  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

  def read_csv_from_file(filepath)
    deprecate('read_csv_from_file -> read')
    self.read(filepath)
  end

  private

  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
end

# Forward port CSV::Cell, as it is sometimes
# serialised in YAML. :-(
class CSV::Cell < String
  def initialize(data = '', is_null = false)
    super(is_null ? '' : data)
  end

  def data
    to_s
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ndr_import-11.2.0 lib/ndr_import/csv_library.rb
ndr_import-11.1.0 lib/ndr_import/csv_library.rb
ndr_import-11.0.2 lib/ndr_import/csv_library.rb
ndr_import-11.0.1 lib/ndr_import/csv_library.rb
ndr_import-11.0.0 lib/ndr_import/csv_library.rb
ndr_import-10.3.0 lib/ndr_import/csv_library.rb
ndr_import-10.2.0 lib/ndr_import/csv_library.rb
ndr_import-10.1.3 lib/ndr_import/csv_library.rb
ndr_import-10.1.2 lib/ndr_import/csv_library.rb
ndr_import-10.1.1 lib/ndr_import/csv_library.rb
ndr_import-10.1 lib/ndr_import/csv_library.rb
ndr_import-10.0 lib/ndr_import/csv_library.rb
ndr_import-9.1.0 lib/ndr_import/csv_library.rb