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

Instance Method Summary collapse

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.

Returns:

  • (Boolean)


9
10
11
# File 'lib/core/facets/filetest/relative.rb', line 9

def absolute?(path)
  !relative?(path)
end

.chop_basename(path) ⇒ Object

List File.split, but preserves the file separators.

FileTest.chop_basename('/usr/lib') #=> ['/usr/', 'lib']
FileTest.chop_basename('/') #=> nil

Returns Array of ‘[pre-basename, basename]` or `nil`.

This method is here simply to support the #relative? and #absolute? methods.



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?

Returns:

  • (Boolean)


6
7
8
9
10
# File 'lib/core/facets/filetest/contains.rb', line 6

def contains?(child, parent=Dir.pwd)
  parent = File.expand_path(parent)
  child = File.expand_path(child)
  child.sub(parent,'') != child
end

.relative?(path) ⇒ Boolean

The opposite of #absolute?

Returns:

  • (Boolean)


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, _ = r
  end
  path == ''
end

.root?(dir = nil) ⇒ Boolean

Is the specified directory the root directory?

CREDIT: Jeffrey Schwab

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'lib/core/facets/filetest/root.rb', line 9

def root?(dir=nil)
  pth = File.expand_path(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! Please help improve if you know how!

Returns [Boolean]

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'lib/core/facets/filetest/safe.rb', line 14

def safe?(path)
  case path
  when /\A(#{SEPARATOR_PATTERN}|\~)(#{SEPARATOR_PATTERN}|\*)+/
    false
  else
    true
  end
end

Instance Method Details

#absolute?(path) ⇒ Object (private)

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 (private)

List File.split, but preserves the file separators.

FileTest.chop_basename('/usr/lib') #=> ['/usr/', 'lib']
FileTest.chop_basename('/') #=> nil

Returns Array of ‘[pre-basename, basename]` or `nil`.

This method is here simply to support the #relative? and #absolute? methods.



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) ⇒ Object (private)

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.expand_path(parent)
  child = File.expand_path(child)
  child.sub(parent,'') != child
end

#relative?(path) ⇒ Object (private)

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, _ = r
  end
  path == ''
end

#root?(dir = nil) ⇒ Object (private)

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.expand_path(dir||Dir.pwd)
  return true if pth == '/'
  return true if pth =~ /^(\w:)?\/$/
  false
end

#safe?(path) ⇒ Object (private)

Is a path considered reasonably “safe”?

Do not mistake this for a perfect solution! Please help improve if you know how!

Returns [Boolean]



14
15
16
17
18
19
20
21
# File 'lib/core/facets/filetest/safe.rb', line 14

def safe?(path)
  case path
  when /\A(#{SEPARATOR_PATTERN}|\~)(#{SEPARATOR_PATTERN}|\*)+/
    false
  else
    true
  end
end