Sha256: 02cc877113840f2ecb1c7a5fb54bf3a13d600f77dc2085e374e8144e84b202ed

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module FileTest

  module_function

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

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

  # Is a file a command executable?
  #
  # TODO: Make more robust. Probably needs to be fixed for Windows.

  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

  # Is a path considered reasonably "safe"?
  #
  # TODO: Make more robust.

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

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

  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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ratch-1.0.0 lib/ratch/core_ext/filetest.rb