Sha256: d5f6e619545c2c0b6130e91b240eda11ae5d1591d3ef2c10a01538936ad77086

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

class String
  def path
    self.extend PathString
  end
end

module PathString
  def to_file option=nil
    raise ArgumentError, "File doesn't exist" if option == :raise && !File.directory?(self)
    File.new(self) if File.file? self
  end
  alias_method :file, :to_file
  alias_method :new_file, :to_file

  def to_dir option=nil
    raise ArgumentError, "Dir doesn't exist" if option == :raise && !File.directory?(self)
    Dir.new(self) if File.directory? self
  end
  alias_method :new_dir, :to_dir
  alias_method :dir, :to_dir

  def to_symlink new_path #, option=nil
    # raise ArgumentError, "New link location doesn't exist" if option == :raise && !File.exist?(new_path)
    File.symlink(self, new_path)
  end
  alias_method :new_symlink, :to_symlink
  alias_method :symlink, :to_symlink

  def exists?
    File.exist? self
  end
  alias_method :there?, :exists?

  def file?
    File.file? self
  end
  alias_method :is_file?, :file?

  def dir?
    File.directory? self
  end
  alias_method :is_dir?, :dir?
  alias_method :directory?, :dir

  def symlink?
    File.symlink? self
  end
  alias_method :is_symlink?, :symlink?

  def up lv
    ('../' * lv) + self
  end

  def down lv
    up_dir = Regexp.escape('../')
    orig = self.clone
    lv.times do
      self.gsub! /^#{up_dir}/, ''
      return self if self == orig
    end
    self
  end

  def post_up lv
    self + ('/..' * lv)
  end

  def post_down lv
    up_dir = Regexp.escape('/..')
    orig = self.clone
    lv.times do
      self.gsub! /#{up_dir}$/, ''
      return self if self == orig
    end
    self
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sugar-high-0.7.3 lib/sugar-high/path.rb
sugar-high-0.7.2 lib/sugar-high/path.rb
sugar-high-0.7.1 lib/sugar-high/path.rb
sugar-high-0.7.0 lib/sugar-high/path.rb