Class: SqlToCsvStream::GzipWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_to_csv_stream/gzip_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ GzipWrapper

Returns a new instance of GzipWrapper.



7
8
9
# File 'lib/sql_to_csv_stream/gzip_wrapper.rb', line 7

def initialize(source)
  @source = source
end

Instance Method Details

#each(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sql_to_csv_stream/gzip_wrapper.rb', line 11

def each(&block)
  @destination = block
  # Zlib::GzipWriter needs to get passed an object that implements the #write method.
  # this is why we implement the #write method further down
  # while assigning the stream we need to write to in an instance variable to be used there.
  @zipper = Zlib::GzipWriter.new(self)
  @source.each do |string|
    @zipper.write(string)
  end
ensure
  @zipper.close
end

#write(zipped_string) ⇒ Object

called indirectly by Zlib::GzipWriter



25
26
27
# File 'lib/sql_to_csv_stream/gzip_wrapper.rb', line 25

def write(zipped_string)
  @destination.yield(zipped_string)
end