lib/file/find.rb in file-find-0.1.0 vs lib/file/find.rb in file-find-0.1.1

- old
+ new

@@ -1,10 +1,10 @@ require 'date' class File::Find # The version of this package - VERSION = '0.1.0' + VERSION = '0.1.1' # :stopdoc: VALID_OPTIONS = %w/ atime ctime @@ -60,15 +60,18 @@ # Windows. # attr_accessor :inum # The name pattern used to limit file searches. The patterns that are legal - # for Dir.glob are legal here. + # for Dir.glob are legal here. The default is '*', i.e. everything. # attr_accessor :name - # Limits searches to files that match the size, in bytes. + # If the value passed is an integer, this option limits searches to files + # that match the size, in bytes, exactly. If a string is passed, you can + # use the standard comparable operators to match files, e.g. ">= 200" would + # limit searches to files greater than or equal to 200 bytes. # attr_accessor :size # Limits searches to files that belong to a specific user ID. # @@ -87,17 +90,17 @@ @ctime = nil @ftype = nil @group = nil @follow = true @inum = nil - @name = nil @size = nil @user = nil validate_and_set_options(options) unless options.empty? @path ||= Dir.pwd + @name ||= '*' end # Executes the find based on the rules you set for the File::Find object. # In block form, yields each file in turn that matches the specified rules. # In non-block form it will return an array of matches instead. @@ -124,18 +127,19 @@ rescue Errno::ELOOP stat_method = :lstat # Handle recursive symlinks retry end + glob = File.join(File.dirname(file), @name) + next unless Dir[glob].include?(file) + # Add directories back onto the list of paths to search unless # they've already been added. # - # TODO: Add depth handling. if stat_info.directory? unless paths.include?(file) paths << file - next end end if @atime date1 = Date.parse(Time.now.to_s) @@ -161,17 +165,26 @@ if @inum next unless stat_info.ino == @inum end end - if @name - glob = File.join(File.dirname(file), @name) - next unless Dir[glob].include?(file) - end - - # TODO: Allow more flexible syntax here, e.g. "> 1024". + # Allow plain numbers, or strings for comparison operators. if @size - next unless stat_info.size == @size + if @size.is_a?(String) + regex = /^([><=]+)\s*?(\d+)$/ + match = regex.match(@size) + + if match.nil? || match.captures.include?(nil) + raise ArgumentError, "invalid size string: '#{@size}'" + end + + operator = match.captures.first.strip + number = match.captures.last.strip.to_i + + next unless stat_info.size.send(operator, number) + else + next unless stat_info.size == @size + end end if @user next unless stat_info.uid == @user end