class Suspect::FileUtils::Idempotent

A humble object class for file system manipulations

Public Instance Methods

file_paths(base_path) click to toggle source
# File lib/suspect/file_utils/idempotent.rb, line 11
def file_paths(base_path)
  result = []

  Find.find(base_path) do |path|
    unless FileTest.directory?(path)
      result << path
    end
  end

  result
rescue Errno::ENOENT # No such file or directory.
  []
end
mkdir(path) click to toggle source
# File lib/suspect/file_utils/idempotent.rb, line 44
def mkdir(path)
  ::FileUtils::mkdir_p path
end
read(path, &block) click to toggle source
# File lib/suspect/file_utils/idempotent.rb, line 25
def read(path, &block)
  if block_given?
    ::File.open(path).each &block
  else
    ::File.open(path) { |f| f.readline }
  end
end
write(path, content) click to toggle source
# File lib/suspect/file_utils/idempotent.rb, line 33
def write(path, content)
  return if path.exist?

  temp_file = ::Tempfile.new
  temp_file.write content
  temp_file.close

  # FileUtils.mv doesn't support --no-clobber option.
  `mv --no-clobber #{temp_file.path} #{path.expand_path}`
end