Sha256: c9d6719dedb5f7e25cd185c3dfb57464c979c1a8238ac361a7c833f261b72790

Contents?: true

Size: 1.88 KB

Versions: 54

Compression:

Stored size: 1.88 KB

Contents

require 'fileutils'

class File
  RELATIVE_PARENTDIR = '..'
  RELATIVE_SAMEDIR = '.'

  # @group Manipulating Paths

  # Turns a path +to+ into a relative path from starting
  # point +from+. The argument +from+ is assumed to be
  # a filename. To treat it as a directory, make sure it
  # ends in +File::SEPARATOR+ ('/' on UNIX filesystems).
  #
  # @param [String] from the starting filename
  #   (or directory with +from_isdir+ set to +true+).
  # @param [String] to the final path that should be made relative.
  # @return [String] the relative path from +from+ to +to+.
  def self.relative_path(from, to)
    from = expand_path(from).split(SEPARATOR)
    to = expand_path(to).split(SEPARATOR)
    from.length.times do
      break if from[0] != to[0]
      from.shift; to.shift
    end
    from.pop
    join(*(from.map { RELATIVE_PARENTDIR } + to))
  end

  # Cleans a path by removing extraneous '..', '.' and '/' characters
  #
  # @example Clean a path
  #   File.cleanpath('a/b//./c/../e') # => "a/b/e"
  # @param [String] path the path to clean
  # @return [String] the sanitized path
  def self.cleanpath(path)
    path = path.split(SEPARATOR)
    path = path.inject([]) do |acc, comp|
      next acc if comp == RELATIVE_SAMEDIR
      if comp == RELATIVE_PARENTDIR && acc.size > 0 && acc.last != RELATIVE_PARENTDIR
        acc.pop
        next acc
      end
      acc << comp
    end
    File.join(*path)
  end

  # @group Reading Files

  # Forces opening a file (for writing) by first creating the file's directory
  # @param [String] file the filename to open
  # @since 0.5.2
  def self.open!(file, *args, &block)
    dir = dirname(file)
    FileUtils.mkdir_p(dir) unless directory?(dir)
    open(file, *args, &block)
  end

  # Reads a file with binary encoding
  # @return [String] the ascii-8bit encoded data
  # @since 0.5.3
  def self.read_binary(file)
    File.open(file, 'rb') {|f| f.read }
  end
end

Version data entries

54 entries across 45 versions & 8 rubygems

Version Path
abaci-0.3.0 vendor/bundle/gems/yard-0.9.1/lib/yard/core_ext/file.rb
abaci-0.3.0 vendor/bundle/gems/yard-0.9.2/lib/yard/core_ext/file.rb
yard-0.9.5 lib/yard/core_ext/file.rb
yard-0.9.4 lib/yard/core_ext/file.rb
yard-0.9.3 lib/yard/core_ext/file.rb
yard-0.9.2 lib/yard/core_ext/file.rb
yard-0.9.1 lib/yard/core_ext/file.rb
yard-0.9.0 lib/yard/core_ext/file.rb
yard-0.8.7.6 lib/yard/core_ext/file.rb
yard-0.8.7.5 lib/yard/core_ext/file.rb
climine-0.0.7 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/core_ext/file.rb
climine-0.0.7 vendor/bundle/ruby/2.1.0/gems/yard-0.8.7.3/lib/yard/core_ext/file.rb
climine-0.0.6 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/core_ext/file.rb
climine-0.0.5 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/core_ext/file.rb
yard-0.8.7.4 lib/yard/core_ext/file.rb
climine-0.0.4 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/core_ext/file.rb
climine-0.0.3 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/core_ext/file.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/yard-0.8.7.3/lib/yard/core_ext/file.rb
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/core_ext/file.rb
mango-0.7.0 vendor/bundler/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/core_ext/file.rb