Class: IOStreams::File::Path

Inherits:
BasePath show all
Defined in:
lib/io_streams/file/path.rb

Instance Attribute Summary

Attributes inherited from BasePath

#path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasePath

#initialize, #join, #reader, #to_s, #writer

Constructor Details

This class inherits a constructor from IOStreams::BasePath

Class Method Details

.mkpath(path) ⇒ Object

Used by writers that can write directly to file to create the target path



23
24
25
26
# File 'lib/io_streams/file/path.rb', line 23

def self.mkpath(path)
  dir = ::File.dirname(path)
  FileUtils.mkdir_p(dir) unless ::File.exist?(dir)
end

.temp_file_name(basename, extension = '') ⇒ Object

Yields the path to a temporary file_name.

File is deleted upon completion if present.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/io_streams/file/path.rb', line 10

def self.temp_file_name(basename, extension = '')
  result = nil
  ::Dir::Tmpname.create([basename, extension]) do |tmpname|
    begin
      result = yield(tmpname)
    ensure
      ::File.unlink(tmpname) if ::File.exist?(tmpname)
    end
  end
  result
end

Instance Method Details

#delete(recursively: false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/io_streams/file/path.rb', line 46

def delete(recursively: false)
  return self unless ::File.exist?(path)

  if ::File.directory?(path)
    recursively ? FileUtils.remove_dir(path) : Dir.delete(path)
  else
    ::File.unlink(path)
  end
  self
end

#exist?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/io_streams/file/path.rb', line 38

def exist?
  ::File.exist?(path)
end

#mkdirObject



33
34
35
36
# File 'lib/io_streams/file/path.rb', line 33

def mkdir
  FileUtils.mkdir_p(path) unless ::File.exist?(path)
  self
end

#mkpathObject



28
29
30
31
# File 'lib/io_streams/file/path.rb', line 28

def mkpath
  self.class.mkpath(path)
  self
end

#sizeObject



42
43
44
# File 'lib/io_streams/file/path.rb', line 42

def size
  ::File.size(path)
end