Replace files atomically in inreplace

This commit is contained in:
Jack Nagel 2014-03-26 16:07:49 -05:00
parent 4fe0be37c0
commit 7b389fbfed

View File

@ -2,12 +2,10 @@ module Utils
module Inreplace
def inreplace paths, before=nil, after=nil
Array(paths).each do |path|
f = File.open(path, 'rb')
s = f.read
s = File.open(path, "rb", &:read)
if before.nil? && after.nil?
s.extend(StringInreplaceExtension)
yield s
yield s.extend(StringInreplaceExtension)
else
after = after.to_s if Symbol === after
unless s.gsub!(before, after)
@ -19,8 +17,7 @@ module Utils
end
end
f.reopen(path, 'wb').write(s)
f.close
Pathname(path).atomic_write(s)
end
end
end