Module: OsCtl::Lib::Utils::File

Defined in:
lib/libosctl/utils/file.rb

Instance Method Summary collapse

Instance Method Details

#regenerate_file(path, mode) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/libosctl/utils/file.rb', line 3

def regenerate_file(path, mode)
  replacement = "#{path}.new"

  File.open(replacement, 'w', mode) do |new|
    if File.exist?(path)
      File.open(path, 'r') do |old|
        yield(new, old)
      end

    else
      yield(new, nil)
    end
  end

  File.rename(replacement, path)
end