Module: FileTest

Defined in:
lib/folio/filetest.rb

Constant Summary collapse

SEPARATOR_PAT =
/#{Regexp.quote File::SEPARATOR}/

Class 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)


13
14
15
# File 'lib/folio/filetest.rb', line 13

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

.bin?(fname) ⇒ Boolean

Is a file a bin/ executable?

TODO: Make more robust. Probably needs to be fixed for Windows.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/folio/filetest.rb', line 34

def bin?(fname)
  is_bin = command_paths.any? do |f|
    FileTest.exist?(File.join(f, fname))
  end
  #is_bin ? File.basename(fname) : false
  is_bin ? fname : false
end

.chop_basename(path) ⇒ Object

Chop_basename(path) -> [pre-basename, basename] or nil



77
78
79
80
81
82
83
84
# File 'lib/folio/filetest.rb', line 77

def chop_basename(path)
  base = File.basename(path)
  if /\A#{SEPARATOR_PAT}?\z/ =~ base
    return nil
  else
    return path[0, path.rindex(base)], base
  end
end

.command_pathsObject

Return a cached list of the PATH environment variable. This is a support method used by #bin?



27
28
29
# File 'lib/folio/filetest.rb', line 27

def command_paths
  @command_paths ||= ENV['PATH'].split(/[:;]/)
end

.out_of_date?(path, *sources) ⇒ Boolean

Does a path need updating, based on given sources? This compares mtimes of give paths. Returns false if the path needs to be updated.

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
# File 'lib/folio/filetest.rb', line 65

def out_of_date?(path, *sources)
  return true unless File.exist?(path)

  sources = sources.collect{ |source| Dir.glob(source) }.flatten
  mtimes  = sources.collect{ |file| File.mtime(file) }

  return true if mtimes.empty?  # TODO: This the way to go here?

  File.mtime(path) < mtimes.max
end

.relative?(path) ⇒ Boolean

The opposite of #absolute?

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/folio/filetest.rb', line 18

def relative?(path)
  while r = chop_basename(path.to_s)
    path, basename = r
  end
  path == ''
end

.safe?(path) ⇒ Boolean

Is a path considered reasonably “safe”?

TODO: Make more robust.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'lib/folio/filetest.rb', line 53

def safe?(path)
  case path
  when *[ '/', '/*', '/**/*' ]
    return false
  end
  true
end