lib/file/find.rb in file-find-0.2.4 vs lib/file/find.rb in file-find-0.2.5

- old
+ new

@@ -4,20 +4,22 @@ include Config include Sys class File::Find # The version of this library - VERSION = '0.2.4' + VERSION = '0.2.5' # :stopdoc: VALID_OPTIONS = %w/ atime ctime follow ftype inum group + maxdepth + mindepth mtime name pattern path perm @@ -70,10 +72,20 @@ # Limits search to a file with a specific inode number. Ignored on MS # Windows. # attr_accessor :inum + # Limits search to a maximum depth into the tree relative to the starting + # search directory. + # + attr_accessor :maxdepth + + # Limits search to a minimum depth into the tree relative to the starting + # search directory. + # + attr_accessor :mindepth + # Limits searches by file modification time, where the value you supply is # the number of days back from the time that the File::Find#find method was # called. # attr_accessor :mtime @@ -145,10 +157,12 @@ @prune = nil @size = nil @user = nil @previous = nil + @maxdepth = nil + @mindepth = nil validate_and_set_options(options) unless options.empty? @path ||= Dir.pwd @name ||= '*' @@ -205,24 +219,46 @@ retry end glob = File.join(File.dirname(file), @name) - # Add directories back onto the list of paths to search unless - # they've already been added. - # - # TODO: Add max_depth support here - # - if stat_info.directory? - unless paths.include?(file) - paths << file - end - end - # Dir[] doesn't like backslashes if File::ALT_SEPARATOR file.tr!(File::ALT_SEPARATOR, File::SEPARATOR) glob.tr!(File::ALT_SEPARATOR, File::SEPARATOR) + end + + if @maxdepth || @mindepth + file_depth = file.split(File::SEPARATOR).length + path_depth = @path.split(File::SEPARATOR).length + depth = file_depth - path_depth + + if @maxdepth && (depth > @maxdepth) + if File.directory?(file) + unless paths.include?(file) && depth > @maxdepth + paths << file + end + end + + next + end + + if @mindepth && (depth < @mindepth) + if File.directory?(file) + unless paths.include?(file) && depth < @mindepth + paths << file + end + end + + next + end + end + + # Add directories back onto the list of paths to search unless + # they've already been added + # + if stat_info.directory? + paths << file unless paths.include?(file) end next unless Dir[glob].include?(file) if @atime