Class: Bundleup::Backup

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*paths) ⇒ Backup

Returns a new instance of Backup.



13
14
15
16
17
# File 'lib/bundleup/backup.rb', line 13

def initialize(*paths)
  @original_contents = paths.each_with_object({}) do |path, hash|
    hash[path] = File.read(path)
  end
end

Class Method Details

.restore_on_error(*paths) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/bundleup/backup.rb', line 3

def self.restore_on_error(*paths)
  backup = new(*paths)
  begin
    yield(backup)
  rescue StandardError, Interrupt
    backup.restore
    raise
  end
end

Instance Method Details

#restoreObject



19
20
21
22
23
# File 'lib/bundleup/backup.rb', line 19

def restore
  original_contents.each do |path, contents|
    File.write(path, contents)
  end
end