Class: PgExport::ValueObjects::DumpFile

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_export/lib/pg_export/value_objects/dump_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(file = Tempfile.new) ⇒ DumpFile

Returns a new instance of DumpFile.



8
9
10
# File 'lib/pg_export/lib/pg_export/value_objects/dump_file.rb', line 8

def initialize(file = Tempfile.new)
  @file = file
end

Instance Method Details

#copy(cipher:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pg_export/lib/pg_export/value_objects/dump_file.rb', line 28

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

#each_chunkObject



45
46
47
48
49
# File 'lib/pg_export/lib/pg_export/value_objects/dump_file.rb', line 45

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

#pathObject



12
13
14
# File 'lib/pg_export/lib/pg_export/value_objects/dump_file.rb', line 12

def path
  file.path
end

#readObject



24
25
26
# File 'lib/pg_export/lib/pg_export/value_objects/dump_file.rb', line 24

def read
  file.read
end

#rewindObject



20
21
22
# File 'lib/pg_export/lib/pg_export/value_objects/dump_file.rb', line 20

def rewind
  file.rewind
end

#sizeObject



16
17
18
# File 'lib/pg_export/lib/pg_export/value_objects/dump_file.rb', line 16

def size
  file.size
end

#size_humanObject



51
52
53
# File 'lib/pg_export/lib/pg_export/value_objects/dump_file.rb', line 51

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

#write(&block) ⇒ Object



41
42
43
# File 'lib/pg_export/lib/pg_export/value_objects/dump_file.rb', line 41

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