Class: Flexdot::Backup

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

Defined Under Namespace

Classes: AlreadyFinishedError

Constant Summary collapse

BASE_DIR =
'backup'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keep_max_count) ⇒ Backup

Returns a new instance of Backup.



22
23
24
25
26
# File 'lib/flexdot/backup.rb', line 22

def initialize(keep_max_count)
  backup_dir.mkpath unless backup_dir.exist?
  @keep_max_count = keep_max_count
  @finished = false
end

Class Method Details

.base_dirObject



17
18
19
# File 'lib/flexdot/backup.rb', line 17

def base_dir
  Pathname.pwd.join(BASE_DIR)
end

.clear_allObject



13
14
15
# File 'lib/flexdot/backup.rb', line 13

def clear_all
  base_dir.glob('*').each(&:rmtree)
end

Instance Method Details

#call(file) ⇒ Object



28
29
30
31
# File 'lib/flexdot/backup.rb', line 28

def call(file)
  raise AlreadyFinishedError if @finished
  FileUtils.mv(file, backup_dir)
end

#finish!Object



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

def finish!
  backup_dir.delete if backup_dir.empty?
  @finished = true
end

#remove_outdated!Object



38
39
40
41
42
43
44
45
46
# File 'lib/flexdot/backup.rb', line 38

def remove_outdated!
  return if @keep_max_count.nil?

  backups = self.class.base_dir.glob('*/').select { |dir| dir.basename.to_s.match?(/\d{14}/) }
  backups.sort_by! { |dir| dir.basename.to_s }.reverse!
  backups.slice!(0, @keep_max_count)

  backups.each(&:rmtree)
end