Sha256: 1390a25110e77f8085d5baaa760e16e0f07239adcf3d2430fd36732d30407953

Contents?: true

Size: 1.97 KB

Versions: 5

Compression:

Stored size: 1.97 KB

Contents

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

5 entries across 5 versions & 1 rubygems

Version Path
tms-1.3.3 lib/tms/path.rb
tms-1.3.2 lib/tms/path.rb
tms-1.3.1.1 lib/tms/path.rb
tms-1.3.1 lib/tms/path.rb
tms-1.3.0 lib/tms/path.rb