Class: Suspect::FileUtils::Idempotent

Inherits:
Object
  • Object
show all
Defined in:
lib/suspect/file_utils/idempotent.rb

Overview

A humble object class for file system manipulations

Instance Method Summary collapse

Instance Method Details

#file_paths(base_path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object



44
45
46
# File 'lib/suspect/file_utils/idempotent.rb', line 44

def mkdir(path)
  ::FileUtils::mkdir_p path
end

#read(path, &block) ⇒ Object



25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# 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