lib/file/find.rb in file-find-0.2.1 vs lib/file/find.rb in file-find-0.2.2
- old
+ new
@@ -1,10 +1,14 @@
require 'date'
+require 'rbconfig'
+require 'sys/admin'
+include Config
+include Sys
class File::Find
- # The version of this package
- VERSION = '0.2.1'
+ # The version of this library
+ VERSION = '0.2.2'
# :stopdoc:
VALID_OPTIONS = %w/
atime
ctime
@@ -43,12 +47,15 @@
# number of days back from the time that the File::Find#find method was
# called.
#
attr_accessor :ctime
- # Limits searches to files that belong to a specific group ID.
+ # Limits searches to files that belong to a specific group, where the
+ # group can be either a group name or ID.
#
+ # Not currently supported on MS Windows.
+ #
attr_accessor :group
# Controls the behavior of how symlinks are followed. If set to true, then
# follows the file pointed to. If false, it considers the symlink itself.
#
@@ -72,10 +79,12 @@
# Limits searches to files which have permissions that match the octal
# value that you provide. For purposes of this comparison, only the user,
# group, and world settings are used. Do not use a leading 0 in the values
# that you supply, e.g. use 755 not 0755.
#
+ # Not currently supported on MS Windows.
+ #
attr_accessor :perm
# Skips files or directories that match the string provided as an argument.
#
attr_accessor :prune
@@ -85,12 +94,15 @@
# 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.
+ # Limits searches to files that belong to a specific user, where the user
+ # can be either a user name or an ID.
#
+ # Not currently supported on MS Windows.
+ #
attr_accessor :user
# The file that matched previously in the current search.
#
attr_reader :previous
@@ -100,10 +112,18 @@
# Creates and returns a new File::Find object. The options set for this
# object serve as the rules for determining what files the File::Find#find
# method will search for.
#
+ # Example:
+ #
+ # rule = File::Find.new(
+ # :name => "*.rb",
+ # :follow => false,
+ # :path => ['/usr/local/lib', '/opt/local/lib']
+ # )
+ #
def initialize(options = {})
@options = options
@atime = nil
@ctime = nil
@@ -126,10 +146,22 @@
# 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.
#
+ # Example:
+ #
+ # rule = File::Find.new(
+ # :name => "*.rb",
+ # :follow => false,
+ # :path => ['/usr/local/lib', '/opt/local/lib']
+ # )
+ #
+ # rule.find{ |f|
+ # puts f
+ # }
+ #
def find
results = [] unless block_given?
paths = @path.to_a
if @prune
@@ -177,10 +209,11 @@
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
next unless Dir[glob].include?(file)
if @atime
@@ -198,14 +231,18 @@
if @ftype
next unless File.ftype(file) == @ftype
end
if @group
- next unless stat_info.gid == @group
+ if @group.is_a?(String)
+ next unless Admin.get_group(stat_info.gid).name == @group
+ else
+ next unless stat_info.gid == @group
+ end
end
- unless RUBY_PLATFORM.match('mswin')
+ unless CONFIG['host_os'] =~ /windows|mswin/i
if @inum
next unless stat_info.ino == @inum
end
end
@@ -235,10 +272,14 @@
next unless stat_info.size == @size
end
end
if @user
- next unless stat_info.uid == @user
+ if @user.is_a?(String)
+ next unless Admin.get_user(stat_info.uid).name == @user
+ else
+ next unless stat_info.uid == @user
+ end
end
if block_given?
yield file
else