lib/file/find.rb in file-find-0.2.5 vs lib/file/find.rb in file-find-0.3.0
- old
+ new
@@ -4,11 +4,11 @@
include Config
include Sys
class File::Find
# The version of this library
- VERSION = '0.2.5'
+ VERSION = '0.3.0'
# :stopdoc:
VALID_OPTIONS = %w/
atime
ctime
@@ -56,10 +56,15 @@
#
# Not currently supported on MS Windows.
#
attr_accessor :group
+ # An array of two element arrays for storing FileTest methods and their
+ # boolean value.
+ #
+ attr_accessor :filetest
+
# Controls the behavior of how symlinks are followed. If set to true (the
# default), then follows the file pointed to. If false, it considers the
# symlink itself.
#
attr_accessor :follow
@@ -133,17 +138,21 @@
# 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.
#
+ # In addition to the standard list of valid options, you may also use
+ # FileTest methods as options, setting their value to true or false.
+ #
# Example:
#
# rule = File::Find.new(
- # :name => "*.rb",
- # :follow => false,
- # :path => ['/usr/local/lib', '/opt/local/lib']
- # )
+ # :name => "*.rb",
+ # :follow => false,
+ # :path => ['/usr/local/lib', '/opt/local/lib'],
+ # :readable? => true
+ # )
#
def initialize(options = {})
@options = options
@atime = nil
@@ -159,10 +168,11 @@
@user = nil
@previous = nil
@maxdepth = nil
@mindepth = nil
+ @filetest = []
validate_and_set_options(options) unless options.empty?
@path ||= Dir.pwd
@name ||= '*'
@@ -260,10 +270,26 @@
paths << file unless paths.include?(file)
end
next unless Dir[glob].include?(file)
+ unless @filetest.empty?
+ file_test = true
+
+ @filetest.each{ |array|
+ meth = array[0]
+ bool = array[1]
+
+ unless File.send(meth, file) == bool
+ file_test = false
+ break
+ end
+ }
+
+ next unless file_test
+ end
+
if @atime
date1 = Date.parse(Time.now.to_s)
date2 = Date.parse(stat_info.atime.to_s)
next unless (date1 - date2).numerator == @atime
end
@@ -354,18 +380,31 @@
end
private
# This validates that the keys are valid. If they are, it sets the value
- # of that key's corresponding method to the given value.
+ # of that key's corresponding method to the given value. If a key ends
+ # with a '?', it's validated as a File method.
#
def validate_and_set_options(options)
options.each do |key, value|
key = key.to_s.downcase
- unless VALID_OPTIONS.include?(key)
- raise ArgumentError, "invalid option '#{key}'"
+
+ if key[-1].chr == '?'
+ sym = key.to_sym
+
+ unless File.respond_to?(sym)
+ raise ArgumentError, "invalid option '#{key}'"
+ end
+
+ @filetest << [sym, value]
+ else
+ unless VALID_OPTIONS.include?(key)
+ raise ArgumentError, "invalid option '#{key}'"
+ end
+
+ send("#{key}=", value)
end
- send("#{key}=", value)
end
end
# Converts a symoblic permissions mode into its octal equivalent.
#--