Sha256: b96c263009692cea5656532c467fbf776deac4d23a538392f29ede8d189758a6

Contents?: true

Size: 764 Bytes

Versions: 5

Compression:

Stored size: 764 Bytes

Contents

# Wraps a CSV object, if wrapper opens the csv file it will close it
class CSVUtils::CSVWrapper
  attr_reader :csv

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

  def self.open(file, mode, csv_options = {})
    csv = new(file, mode, csv_options)

    if block_given?
      yield csv
      csv.close
    else
      csv
    end
  end

  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

  def <<(row)
    csv << row
  end

  def shift
    csv.shift
  end

  def close
    csv.close if close_when_done?
  end

  private

  def close_when_done?
    @close_when_done
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
csv-utils-0.3.4 lib/csv_utils/csv_wrapper.rb
csv-utils-0.3.3 lib/csv_utils/csv_wrapper.rb
csv-utils-0.3.2 lib/csv_utils/csv_wrapper.rb
csv-utils-0.3.1 lib/csv_utils/csv_wrapper.rb
csv-utils-0.3.0 lib/csv_utils/csv_wrapper.rb