Class: FilePath
- Inherits:
-
String
- Object
- String
- FilePath
- 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
-
.current ⇒ Object
Path to current file: File.expand_path(__FILE__).
Instance Method Summary collapse
-
#/(other) ⇒ Object
Joins string or filepath into new path Example: FilePath.new(“/somedir”)/“other_dir”/“file” => “/somedir/other_dir/file”.
-
#basename(suffix = "") ⇒ Object
Returns string with basename like in File.basename.
-
#dirname ⇒ Object
Returns file path as in File.dirname but of FilePath type.
-
#include_path?(filepath) ⇒ Boolean
Returns
trueif wildcard selection includes specified path. -
#initialize(path = nil) ⇒ FilePath
constructor
Creates new file path.
-
#list ⇒ Object
Returns list of paths selected by wildcard or an array with one specified file if it exists.
-
#wildcard? ⇒ Boolean
Returns
trueif file path is a wildcard.
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 (path) else replace (File.dirname(caller(1)[0].split(":")[0])) end end |
Class Method Details
.current ⇒ Object
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((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 |
#dirname ⇒ Object
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
55 56 57 |
# File 'lib/file_path.rb', line 55 def include_path?(filepath) list.include?((filepath)) end |
#list ⇒ Object
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
44 45 46 |
# File 'lib/file_path.rb', line 44 def wildcard? include?("*") || include?("?") end |