Class: Tobacco::SafetyNet
- Inherits:
-
Object
- Object
- Tobacco::SafetyNet
- Defined in:
- lib/tobacco/safety_net.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#backup ⇒ Object
Public: Creates a backup of the original file so it can be restored if necessary.
-
#destination ⇒ Object
Public: Memoizes the destination path.
-
#destroy ⇒ Object
Public: Destroys the backup file.
-
#initialize(filepath) ⇒ SafetyNet
constructor
A new instance of SafetyNet.
-
#restore ⇒ Object
Public: Restores the original file in the event an error occurs during writing the new content.
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
#file ⇒ Object (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
#backup ⇒ Object
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 |
#destination ⇒ Object
Public: Memoizes the destination path
48 49 50 |
# File 'lib/tobacco/safety_net.rb', line 48 def destination @destination ||= destination_path end |
#destroy ⇒ Object
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 |
#restore ⇒ Object
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 |