Class: GDK::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/gdk/backup.rb

Constant Summary collapse

SourceFileOutsideOfGdk =
Class.new(StandardError)
SourceFileDoesntExist =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file) ⇒ Backup

Returns a new instance of Backup.



16
17
18
19
20
# File 'lib/gdk/backup.rb', line 16

def initialize(source_file)
  @source_file = Pathname.new(source_file.to_s).expand_path

  validate!
end

Instance Attribute Details

#source_fileObject (readonly)

Returns the value of attribute source_file.



10
11
12
# File 'lib/gdk/backup.rb', line 10

def source_file
  @source_file
end

Class Method Details

.backup_rootObject



12
13
14
# File 'lib/gdk/backup.rb', line 12

def self.backup_root
  GDK.root.join('.backups')
end

Instance Method Details

#backup!(advise: true) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/gdk/backup.rb', line 22

def backup!(advise: true)
  ensure_backup_directory_exists
  make_backup_of_source_file
  advise_user if advise

  true
end

#destination_fileObject



30
31
32
# File 'lib/gdk/backup.rb', line 30

def destination_file
  @destination_file ||= backup_root.join("#{relative_source_file.to_s.gsub('/', '__')}.#{Time.now.strftime('%Y%m%d%H%M%S')}")
end

#recover_cmd_stringObject



38
39
40
41
42
43
# File 'lib/gdk/backup.rb', line 38

def recover_cmd_string
  <<~CMD
    cp -f '#{destination_file}' \\
    '#{source_file}'
  CMD
end

#relative_source_fileObject



34
35
36
# File 'lib/gdk/backup.rb', line 34

def relative_source_file
  @relative_source_file ||= source_file.relative_path_from(GDK.root)
end