Class: File

Inherits:
Object show all
Defined in:
lib/coo-coo/core_ext.rb

Class Method Summary collapse

Class Method Details

.write_to(path, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/coo-coo/core_ext.rb', line 50

def self.write_to(path, &block)
  tmp = path.to_s + ".tmp"
  bak = path.to_s + "~"

  # write to temp file
  File.open(tmp, "w", &block)

  # create a backup file
  if File.exists?(path)
    # remove any existing backup
    if File.exists?(bak)
      File.delete(bak)
    end

    File.rename(path, bak)
  end

  # finalize the save
  File.rename(tmp, path)
  
  self
rescue
  File.delete(tmp)
  raise
end