Sha256: 30307ea219b1ee90317b69e4e8e6fd203e70d7b6c57556bf6d850ea21d6f46bc

Contents?: true

Size: 876 Bytes

Versions: 8

Compression:

Stored size: 876 Bytes

Contents

# Builds a csv file from csv rows
module CSVUtils
  class CSVReport
    attr_reader :csv,
                :must_close

    def initialize(csv, headers = nil, csv_options = {}, &block)
      @csv =
        if csv.is_a?(String)
          @must_close = true
          mode = csv_options.delete(:mode) || 'wb'
          CSV.open(csv, mode, csv_options)
        else
          @must_close = false
          csv
        end

      generate(headers, &block) if block
    end

    def generate(headers = nil)
      add_headers(headers) if headers
      yield self
      @csv.close if @must_close
    end

    def append(csv_row)
      @csv <<
        if csv_row.is_a?(Array)
          csv_row
        else
          csv_row.to_a
        end
    end
    alias << append

    def add_headers(csv_row)
      append(csv_row.is_a?(Array) ? csv_row : csv_row.csv_headers)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
csv-utils-0.3.6 lib/csv_utils/csv_report.rb
csv-utils-0.3.5 lib/csv_utils/csv_report.rb
csv-utils-0.3.4 lib/csv_utils/csv_report.rb
csv-utils-0.3.3 lib/csv_utils/csv_report.rb
csv-utils-0.3.2 lib/csv_utils/csv_report.rb
csv-utils-0.3.1 lib/csv_utils/csv_report.rb
csv-utils-0.3.0 lib/csv_utils/csv_report.rb
csv-utils-0.2.3 lib/csv_utils/csv_report.rb