require 'repertoire/media' module Repertoire class Repository # Path to the directory attr_reader :path # Media type of the directory attr_reader :media # # Creates a new Repository object with the specified _path_ and _media_ # type. If a _block_ is given, it will be passed the newly created # Repository object. # # Repository.new('path/to/repo',Media::SVN) # # Repository.new('path/to/svn_repo',Media::SVN) do |dir| # puts dir['**/'] # end # def initialize(path,media=nil,&block) @path = File.expand_path(path) @media = nil if media if File.directory?(File.join(path,media.directory)) @media = media end end block.call(self) if block end # # Similar to Dir.glob, except the media's directory is omitted # from the returned results. If a _block_ is given, it will be passed # each resulting path. # # dir = Directory.new('path/to/my_svn') # dir.glob('sub_path/.svn/*') # => [] # dir.glob('sub_path/**/') # => [...] # def glob(pattern,flags=0,&block) pattern = File.expand_path(File.join(@path,pattern)) paths = filter(Dir.glob(pattern,flags)) paths.each(&block) if block return paths end # # Returns +true+ if the directory contains the specified _sub_path_, # returns +false+ otherwise. # def has_path?(sub_path) !(glob(sub_path).empty?) end # # Returns an +Array+ of the files matching the _sub_path_ within the # directory. # def files(sub_path='*') glob(sub_path).select { |path| File.file?(path) } end # # Returns +true+ if there is a file with the _sub_path_ within the # directory, returns +false+ otherwise. # def has_file?(sub_path) !(files(sub_path).empty?) end # # Returns an +Array+ of the directories matching the _sub_path_ within # the directory. # def directories(sub_path='*') glob(sub_path).select { |path| File.directory?(path) } end # # Returns +true+ if there is a directory with the _sub_path_ within the # directory, returns +false+ otherwise. # def has_directory?(sub_path) !(directories(sub_path).empty?) end # # See glob. # def [](pattern='') glob(pattern) end # # Returns the path of the directory in +String+ form. # def to_s @path.to_s end protected # # Filter the specified _paths_, removing any path that contains # the media's directory. # def filter(paths) return paths unless @media return paths.select do |path| !(path.split(File::SEPARATOR).include?(@media.directory)) end end end end