Sha256: 20261f8dc764e7ba8f97bb089f8700731fd7aeff396725c4c7e1f8f4b2287361

Contents?: true

Size: 1.98 KB

Versions: 11

Compression:

Stored size: 1.98 KB

Contents

# encoding: UTF-8

require 'find'

module Tms
  # cleaned up Pathname
  class Path
    attr_reader :path
    def initialize(*parts)
      @path = File.join(*parts)
    end

    def /(other)
      self.class.new(@path, other)
    end

    def hash
      @path.hash
    end

    def ==(other)
      return false unless Path === other
      other.to_s == @path
    end
    alias_method :===, :==
    alias_method :eql?, :==

    def <=>(other)
      return nil unless Path === other
      @path <=> other.to_s
    end

    def basename(*args)
      self.class.new(File.basename(@path, *args))
    end

    def dirname(*args)
      self.class.new(File.dirname(@path))
    end

    def readlink
      self.class.new(File.readlink(@path))
    end

    def ftype
      File.ftype(@path)
    end

    def lstat
      File.lstat(@path)
    end

    def size
      File.size(@path)
    end

    def size_if_real_file
      file? && !symlink? ? File.size(@path) : 0
    end

    def exist?
      File.exist?(@path)
    end

    def file?
      File.file?(@path)
    end

    def directory?
      File.directory?(@path)
    end

    def symlink?
      File.symlink?(@path)
    end

    def readable_real?
      File.readable_real?(@path)
    end

    def children(with_directory = true)
      with_directory = false if @path == '.'
      result = []
      Dir.foreach(@path) do |e|
        next if e == '.' || e == '..'
        if with_directory
          result << self.class.new(File.join(@path, e))
        else
          result << self.class.new(e)
        end
      end
      result
    end

    def find(&block)
      if @path == '.'
        Find.find(@path){ |f| yield self.class.new(f.sub(/^.\//, '')) }
      else
        Find.find(@path){ |f| yield self.class.new(f) }
      end
    end

    def to_s
      @path.dup
    end
    alias_method :to_str, :to_s
    alias_method :to_path, :to_s

    def postfix
      case
      when symlink?
        '@'
      when directory?
        '/'
      else
        ''
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tms-1.7.4 lib/tms/path.rb
tms-1.7.3 lib/tms/path.rb
tms-1.7.2 lib/tms/path.rb
tms-1.7.1 lib/tms/path.rb
tms-1.7.0 lib/tms/path.rb
tms-1.6.0 lib/tms/path.rb
tms-1.5.0 lib/tms/path.rb
tms-1.4.2 lib/tms/path.rb
tms-1.4.0 lib/tms/path.rb
tms-1.3.5 lib/tms/path.rb
tms-1.3.4 lib/tms/path.rb