Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gzip.rb « csv_builder « lib « csv_builder « gems - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 83a83970acdea2e1d4852933ce076cbbc4bad923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

module CsvBuilder
  class Gzip < CsvBuilder::Builder
    # Writes the CSV file compressed and yields the written tempfile and rows written.
    #
    #
    # Example:
    # > CsvBuilder::Gzip.new(Issue, { title: -> (row) { row.title.upcase }, id: :id }).render do |tempfile, rows|
    # >   puts tempfile.path
    # >   puts `zcat #{tempfile.path}`
    # >   puts rows
    # > end
    def render
      Tempfile.create(['csv_builder_gzip', '.csv.gz']) do |tempfile|
        Zlib::GzipWriter.open(tempfile.path) do |gz|
          csv = CSV.new(gz)

          write_csv csv, until_condition: -> {} # truncation must be handled outside of the CsvBuilder

          csv.close
        end

        yield tempfile, @rows_written
      end
    end
  end
end