Sha256: 19cc15bb80b8eead4dc29db348d5eda9e879ddd930a98788c3b301f1fa55acd1

Contents?: true

Size: 1.94 KB

Versions: 16

Compression:

Stored size: 1.94 KB

Contents

# = pathlist.rb
#
# == Copyright (c) 2005 Jonas Pfenniger
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.
#
# == Author(s)
#
# * Jonas Pfenniger

# Author::    Jonas Pfenniger
# Copyright:: Copyright (c) 2005 Jonas Pfenniger
# License::   Ruby License

# = PathList
#
# A Path is an array containing 1..n paths. It is useful to regroup paths and
# make lookups on them.
#
# == Usage
#
#  path = PathList.new(ENV['PATH'])
#  path.find 'env'            #=> "/usr/bin/env"
#
#  # This is already done when including the library
#  $:.class                   #=> Array
#  $:.extend PathList::Finder
#  $:.find_ext = 'rb'
#
#  $:.find 'uri'              #=> "/usr/lib/ruby/1.8/uri.rb"
#

class PathList < Array

  def initialize(paths, default_ext = nil)
    @find_ext = default_ext
    if paths.kind_of? String
      paths = paths.split(File::PATH_SEPARATOR)
    end
    super(paths)
  end

  def to_s
    join(File::PATH_SEPARATOR)
  end

  module Finder

    attr_accessor :find_ext

    def find(filename, use_ext=true)
      filename += '.' + @find_ext if @find_ext and use_ext
      to_a.each do |path|
        filepath = File.join(path, filename)
        return filepath if File.exist?( filepath )
      end
      return nil
    end
    alias :include? find

  end
  include Finder
end

$:.extend PathList::Finder
$:.find_ext = 'rb'

# Doesn't work
#ENV['PATH'] = PathList.new(ENV['PATH'])

#++


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin #testing

  require 'test/unit'

  # Needs a Mockup
  # It is not possible to test file lookups in a platform independent manner.

  class TC_PathList < Test::Unit::TestCase

  end

=end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
facets-1.4.0 lib/facets/more/pathlist.rb
facets-1.4.1 lib/facets/more/pathlist.rb
facets-1.4.2 lib/facets/more/pathlist.rb
facets-1.4.3 lib/facets/more/pathlist.rb
facets-1.4.4 lib/facets/more/pathlist.rb
facets-1.4.5 lib/facets/more/pathlist.rb
facets-1.7.0 lib/facets/more/pathlist.rb
facets-1.7.30 lib/facets/more/pathlist.rb
facets-1.7.38 lib/facets/more/pathlist.rb
facets-1.7.46 lib/facets/more/pathlist.rb
facets-1.8.0 lib/facets/more/pathlist.rb
facets-1.8.20 lib/facets/more/pathlist.rb
facets-1.8.49 lib/facets/more/pathlist.rb
facets-1.8.51 lib/facets/more/pathlist.rb
facets-1.8.54 lib/facets/more/pathlist.rb
facets-1.8.8 lib/facets/more/pathlist.rb