Class: FilePath

Inherits:
String
  • Object
show all
Defined in:
lib/file_path.rb

Overview

Class FilePath is used for comfortable work with file paths.

Following File class methods are implemented:

filepath.blockdev?        => File.blockdev?(filepath)
filepath.chardev?         => File.chardev?(filepath)
filepath.directory?       => File.directory?(filepath)
filepath.executable?      => File.executable?(filepath)
filepath.executable_real? => File.executable_real?(filepath)
filepath.exist?           => File.exist?(filepath)
filepath.file?            => File.file?(filepath)
filepath.grpowned?        => File.grpowned?(filepath)
filepath.owned?           => File.owned?(filepath)
filepath.pipe?            => File.pipe?(filepath)
filepath.readable?        => File.readable?(filepath)
filepath.readable_real?   => File.readable_real?(filepath)
filepath.setgid?          => File.setgid?(filepath)
filepath.setuid?          => File.setuid?(filepath)
filepath.size?            => File.size?(filepath)
filepath.socket?          => File.socket?(filepath)
filepath.sticky?          => File.sticky?(filepath)
filepath.symlink?         => File.symlink?(filepath)
filepath.writable?        => File.writable?(filepath)
filepath.writable_real?   => File.writable_real?(filepath)
filepath.zero?            => File.zero?(filepath)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ FilePath

Creates new file path. If path param is not given, dirname of current file is used



28
29
30
31
32
33
34
# File 'lib/file_path.rb', line 28

def initialize(path = nil)
  if path
    replace expand(path)
  else
    replace expand(File.dirname(caller(1)[0].split(":")[0]))
  end
end

Class Method Details

.currentObject

Path to current file: File.expand_path(__FILE__)



82
83
84
# File 'lib/file_path.rb', line 82

def current
  new(caller(1)[0].split(":")[0])
end

Instance Method Details

#/(other) ⇒ Object

Joins string or filepath into new path Example:

FilePath.new("/somedir")/"other_dir"/"file"     => "/somedir/other_dir/file"


39
40
41
# File 'lib/file_path.rb', line 39

def /(other)
  self.class.new(expand(File.join(self, other)))
end

#basename(suffix = "") ⇒ Object

Returns string with basename like in File.basename



65
66
67
# File 'lib/file_path.rb', line 65

def basename(suffix = "")
  File.basename(self, suffix)
end

#dirnameObject

Returns file path as in File.dirname but of FilePath type



60
61
62
# File 'lib/file_path.rb', line 60

def dirname
  self.class.new(File.dirname(self))
end

#include_path?(filepath) ⇒ Boolean

Returns true if wildcard selection includes specified path

Returns:

  • (Boolean)


55
56
57
# File 'lib/file_path.rb', line 55

def include_path?(filepath)
  list.include?(expand(filepath))
end

#listObject

Returns list of paths selected by wildcard or an array with one specified file if it exists



50
51
52
# File 'lib/file_path.rb', line 50

def list
  Dir.glob(self).map{ |p| self.class.new(p)}
end

#wildcard?Boolean

Returns true if file path is a wildcard

Returns:

  • (Boolean)


44
45
46
# File 'lib/file_path.rb', line 44

def wildcard?
  include?("*") || include?("?")
end