Module: Dir::MakeDirs

Defined in:
lib/vex/base/filesystem/make_dirs.rb

Instance Method Summary collapse

Instance Method Details

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)

2
3
4
5
6
7
8
# File 'lib/vex/base/filesystem/make_dirs.rb', line 2

def exists?(path)
  begin
    Dir.open(path) && true
  rescue Errno::ENOENT, Errno::ENOTDIR
    false
  end
end

#mkdirs(path) ⇒ Object


10
11
12
13
14
15
16
17
18
# File 'lib/vex/base/filesystem/make_dirs.rb', line 10

def mkdirs(path)
  paths = path.split("/")
  paths.each_with_index do |path, idx|
    p = paths[0..idx].join("/")
    next if p.empty?      # This is root
    next if exists?(p)
    mkdir(p)
  end
end

#rmdirs(path) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vex/base/filesystem/make_dirs.rb', line 20

def rmdirs(path)
  Dir.glob("#{path}/**/*", File::FNM_DOTMATCH).sort.reverse.each do |file|
    if File.directory?(file)
      next if file =~ /\/\.\.?$/
      Dir.rmdir(file)
    else
      File.unlink(file)
    end
  end

  Dir.rmdir(path)
end

#tmp(do_unlink = true, &block) ⇒ Object


33
34
35
36
37
38
39
40
# File 'lib/vex/base/filesystem/make_dirs.rb', line 33

def tmp(do_unlink = true, &block)
  path = "#{App.tmpdir}/#{$$}_#{Thread.current.object_id}"
  Dir.mkdirs path
  
  r = yield(path)
ensure
  Dir.rmdirs(path) if path && do_unlink
end