Class: PgExport::Dump

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Roles::HumanReadable
Defined in:
lib/pg_export/dump.rb

Constant Summary collapse

TIMESTAMP =
'_%Y%m%d_%H%M%S'.freeze
CHUNK_SIZE =
(2**16).freeze

Constants included from Roles::HumanReadable

Roles::HumanReadable::MAPPING

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Roles::HumanReadable

#size_human

Constructor Details

#initialize(name:, db_name:) ⇒ Dump

Returns a new instance of Dump.



19
20
21
22
# File 'lib/pg_export/dump.rb', line 19

def initialize(name:, db_name:)
  @name, @db_name = name, db_name
  @timestamp = Time.now.strftime(TIMESTAMP)
end

Instance Attribute Details

#db_nameObject (readonly)

Returns the value of attribute db_name.



17
18
19
# File 'lib/pg_export/dump.rb', line 17

def db_name
  @db_name
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/pg_export/dump.rb', line 17

def name
  @name
end

Instance Method Details

#each_chunkObject



36
37
38
39
40
# File 'lib/pg_export/dump.rb', line 36

def each_chunk
  open(:read) do |file|
    yield file.read(CHUNK_SIZE) until file.eof?
  end
end

#extObject



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

def ext
  ''
end

#open(operation_type, &block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/pg_export/dump.rb', line 28

def open(operation_type, &block)
  case operation_type.to_sym
  when :read then File.open(path, 'r', &block)
  when :write then File.open(path, 'w', &block)
  else raise ArgumentError, 'Operation type can be only :read or :write'
  end
end

#timestamped_nameObject



42
43
44
# File 'lib/pg_export/dump.rb', line 42

def timestamped_name
  db_name + timestamp + ext
end

#to_sObject



46
47
48
# File 'lib/pg_export/dump.rb', line 46

def to_s
  "#{name} (#{size_human})"
end