Class: Tobacco::SafetyNet

Inherits:
Object
  • Object
show all
Defined in:
lib/tobacco/safety_net.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ SafetyNet

Returns a new instance of SafetyNet.



9
10
11
# File 'lib/tobacco/safety_net.rb', line 9

def initialize(filepath)
  @filepath = filepath
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/tobacco/safety_net.rb', line 7

def file
  @file
end

Instance Method Details

#backupObject

Public: Creates a backup of the original file

so it can be restored if necessary


16
17
18
19
20
21
22
# File 'lib/tobacco/safety_net.rb', line 16

def backup
  Tobacco.log("Backup: #{@filepath} => #{destination}")

  if File.exists? @filepath
    FileUtils.mv(@filepath, destination)
  end
end

#destinationObject

Public: Memoizes the destination path



48
49
50
# File 'lib/tobacco/safety_net.rb', line 48

def destination
  @destination ||= destination_path
end

#destroyObject

Public: Destroys the backup file



38
39
40
41
42
43
44
# File 'lib/tobacco/safety_net.rb', line 38

def destroy
  Tobacco.log("Destroying: #{destination}")

  if File.exists? destination
    FileUtils.rm(destination)
  end
end

#restoreObject

Public: Restores the original file in

the event an error occurs during
writing the new content


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

def restore
  Tobacco.log("Restoring: #{destination} => #{@filepath}")

  if File.exists? destination
    FileUtils.mv(destination, @filepath)
  end
end