Module: FileTest
- Defined in:
- lib/core/facets/filetest/root.rb,
lib/core/facets/filetest/safe.rb,
lib/core/facets/filetest/contains.rb,
lib/core/facets/filetest/relative.rb,
lib/core/facets/filetest/separator_pattern.rb
Constant Summary collapse
- SEPARATOR_PATTERN =
( if File::ALT_SEPARATOR /[#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}]/ else /#{Regexp.quote File::SEPARATOR}/ end ).freeze
Class Method Summary collapse
-
.absolute?(path) ⇒ Boolean
Predicate method for testing whether a path is absolute.
-
.chop_basename(path) ⇒ Object
List File.split, but preserves the file separators.
-
.contains?(child, parent = Dir.pwd) ⇒ Boolean
Does the
parent
contain thechild
?. -
.relative?(path) ⇒ Boolean
The opposite of #absolute?.
-
.root?(dir = nil) ⇒ Boolean
Is the specified directory the root directory?.
-
.safe?(path) ⇒ Boolean
Is a path considered reasonably “safe”?.
Class Method Details
.absolute?(path) ⇒ Boolean
Predicate method for testing whether a path is absolute. It returns true
if the pathname begins with a slash.
9 10 11 |
# File 'lib/core/facets/filetest/relative.rb', line 9 def absolute?(path) !relative?(path) end |
.chop_basename(path) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/core/facets/filetest/relative.rb', line 29 def chop_basename(path) base = File.basename(path) if /\A#{SEPARATOR_PATTERN}?\z/ =~ base return nil else return path[0, path.rindex(base)], base end end |
.contains?(child, parent = Dir.pwd) ⇒ Boolean
Does the parent
contain the child
?
6 7 8 9 10 |
# File 'lib/core/facets/filetest/contains.rb', line 6 def contains?(child, parent=Dir.pwd) parent = File.(parent) child = File.(child) child.sub(parent,'') != child end |
.relative?(path) ⇒ Boolean
The opposite of #absolute?
14 15 16 17 18 19 |
# File 'lib/core/facets/filetest/relative.rb', line 14 def relative?(path) while r = chop_basename(path.to_s) path, basename = r end path == '' end |
.root?(dir = nil) ⇒ Boolean
Is the specified directory the root directory?
CREDIT: Jeffrey Schwab
9 10 11 12 13 14 |
# File 'lib/core/facets/filetest/root.rb', line 9 def root?(dir=nil) pth = File.(dir||Dir.pwd) return true if pth == '/' return true if pth =~ /^(\w:)?\/$/ false end |
.safe?(path) ⇒ Boolean
Is a path considered reasonably “safe”?
Do not mistake this for a perfect solution!
10 11 12 13 14 15 16 17 |
# File 'lib/core/facets/filetest/safe.rb', line 10 def safe?(path) case path when /\A(#{SEPARATOR_PATTERN}|\~)(#{SEPARATOR_PATTERN}|\*)+/ false else true end end |