Sha256: 1a3ccfad8eda9c353812366d37c63b793c94cf6b0a46dcd8ff8cf1c295396427

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'tempfile'

class PgExport
  module ValueObjects
    class DumpFile
      def initialize(file = Tempfile.new)
        @file = file
      end

      def path
        file.path
      end

      def size
        file.size
      end

      def rewind
        file.rewind
      end

      def read
        file.read
      end

      def copy(cipher:)
        cipher.reset
        new_self = self.class.new
        new_self.write do |f|
          each_chunk do |chunk|
            f << cipher.update(chunk)
          end
          f << cipher.final
        end

        new_self
      end

      def write(&block)
        File.open(path, 'w', &block)
      end

      def each_chunk
        File.open(path, 'r') do |file|
          yield file.read(CHUNK_SIZE) until file.eof?
        end
      end

      def size_human
        MAPPING.each_pair { |e, s| return "#{(size.to_f / (s / 1024)).round(2)}#{e}" if size < s }
      end

      private

      CHUNK_SIZE = (2**16)
      MAPPING = {
        'B' => 1024,
        'kB' => 1024 * 1024,
        'MB' => 1024 * 1024 * 1024,
        'GB' => 1024 * 1024 * 1024 * 1024,
        'TB' => 1024 * 1024 * 1024 * 1024 * 1024
      }.freeze
      private_constant :CHUNK_SIZE, :MAPPING

      attr_reader :file
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pg_export-1.0.0 lib/pg_export/value_objects/dump_file.rb
pg_export-1.0.0.rc8 lib/pg_export/value_objects/dump_file.rb
pg_export-1.0.0.rc6 lib/pg_export/lib/pg_export/value_objects/dump_file.rb