Class: Cfruby::FileFind::FileMatch
- Inherits:
-
Object
- Object
- Cfruby::FileFind::FileMatch
- Defined in:
- lib/libcfruby/filefind.rb
Overview
The FileMatch class implements a matches? method which is called on each file in turn during a directory traversal. The class encapsulates the logic necessary to determine which files should be included in Find output and which should not.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ FileMatch
constructor
- Options:
:glob
- can be a standard file glob (*.txt) or a regular expression or an array of either
:directoriesonly
- set to true will cause only directories to be matched
:filesonly
- set to true will cause only files to be matched
:exclude
- exclude any files matching the given pattern or patterns (may be standard file globs or regular expressions)
:include
- overrides excluded files selectively
:newerthan
- only files younger than this
:olderthan
- only files older than this
:empty
- true if the file is 0 length or the directory is empty or a symlink is broken
:brokenlink
- true if a symlink is broken
:largerthan
- only files larger than this (bytes, or /([0-9]+)(kmg)b?/)
:smallerthan
-
only files smaller than this (bytes, or /([0-9]+)(kmg)b?/).
- only files larger than this (bytes, or /([0-9]+)(kmg)b?/)
- true if a symlink is broken
- true if the file is 0 length or the directory is empty or a symlink is broken
- only files older than this
- only files younger than this
- overrides excluded files selectively
- exclude any files matching the given pattern or patterns (may be standard file globs or regular expressions)
- set to true will cause only files to be matched
- set to true will cause only directories to be matched
- can be a standard file glob (*.txt) or a regular expression or an array of either
- Options:
- #matches?(filename) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ FileMatch
Options:
:glob
-
can be a standard file glob (*.txt) or a regular expression or an array of either
:directoriesonly
-
set to true will cause only directories to be matched
:filesonly
-
set to true will cause only files to be matched
:exclude
-
exclude any files matching the given pattern or patterns (may be standard file globs or regular expressions)
:include
-
overrides excluded files selectively
:newerthan
-
only files younger than this
:olderthan
-
only files older than this
:empty
-
true if the file is 0 length or the directory is empty or a symlink is broken
:brokenlink
-
true if a symlink is broken
:largerthan
-
only files larger than this (bytes, or /([0-9]+)(kmg)b?/)
:smallerthan
-
only files smaller than this (bytes, or /([0-9]+)(kmg)b?/)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/libcfruby/filefind.rb', line 41 def initialize( = {}) @globs = Array.new() @regex = Array.new() if([:glob]) if([:glob].kind_of?(String)) Cfruby.controller.inform('debug', "adding \"#{[:glob].to_s}\" to search parameters") @globs << [:glob] elsif([:glob].kind_of?(Regexp)) Cfruby.controller.inform('debug', "adding \"#{[:glob].to_s}\" to search parameters as a regular expression") @regex << [:glob] elsif([:glob][0].kind_of?(String)) [:glob].each() { |subglob| Cfruby.controller.inform('debug', "adding \"#{subglob.to_s}\" to search parameters") @globs << subglob } elsif([:glob][0].kind_of?(Regexp)) [:glob].each() { |subglob| Cfruby.controller.inform('debug', "adding \"#{subglob.to_s}\" to search parameters as a regular expression") @regex << subglob } end end @matchmethods = Array.new() if([:directoriesonly]) Cfruby.controller.inform('debug', "limiting search to directories only") @matchmethods << :directoriesonly elsif([:filesonly]) Cfruby.controller.inform('debug', "limiting search to files only") @matchmethods << :filesonly end if([:olderthan]) if(![:olderthan].kind_of?(Time)) @olderthan = Time.parse([:olderthan]) else @olderthan = [:olderthan] end Cfruby.controller.inform('debug', "limiting search to items older than #{@olderthan.to_s}") @matchmethods << :olderthan end if([:smallerthan]) @smallerthan = convertsize([:smallerthan]) Cfruby.controller.inform('debug', "limiting search to items smaller than #{@smallerthan} bytes") @matchmethods << :smallerthan end if([:largerthan]) @largerthan = convertsize([:largerthan]) Cfruby.controller.inform('debug', "limiting search to items larger than #{@largerthan} bytes") @matchmethods << :largerthan end if([:empty]) @matchmethods << :empty end if([:brokenlink]) @matchmethods << :brokenlink end @excludeglobs = Array.new() @excluderegex = Array.new() @includeglobs = Array.new() @includeregex = Array.new() if([:exclude]) if([:exclude].kind_of?(String)) Cfruby.controller.inform('debug', "excluding items matching #{[:exclude]}") @excludeglobs << [:exclude] elsif([:exclude].kind_of?(Regexp)) Cfruby.controller.inform('debug', "excluding items matching #{[:exclude]} as a regular expression") @excluderegex << [:exclude] elsif([:exclude][0].kind_of?(String)) [:exclude].each() { |subglob| Cfruby.controller.inform('debug', "excluding items matching #{subglob} as a regular expression") @excludeglobs << subglob } elsif([:exclude][0].kind_of?(Regexp)) [:exclude].each() { |subglob| Cfruby.controller.inform('debug', "excluding items matching #{subglob}") @excluderegex << subglob } end end if([:include]) if([:include].kind_of?(String)) Cfruby.controller.inform('debug', "including items matching #{[:include]}") @includeglobs << [:include] elsif([:include].kind_of?(Regexp)) Cfruby.controller.inform('debug', "including items matching #{[:include]} as a regular expression") @includeregex << [:include] elsif([:include][0].kind_of?(String)) [:include].each() { |subglob| Cfruby.controller.inform('debug', "including items matching #{subglob}") @includeglobs << subglob } elsif([:include][0].kind_of?(Regexp)) [:include].each() { |subglob| Cfruby.controller.inform('debug', "including items matching #{subglob} as a regular expression") @includeregex << subglob } end end end |
Instance Method Details
#matches?(filename) ⇒ Boolean
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/libcfruby/filefind.rb', line 149 def matches?(filename) basename = filename.basename # binary conditions @matchmethods.each() { |method| if(!self.send(method, filename)) return(false) end } # basic matching @globs.each() { |glob| if(!File.fnmatch(glob, basename, File::FNM_DOTMATCH)) return(false) end } @regex.each() { |regex| if(!regex.match(basename)) return(false) end } # exclude/include exclude = false @excludeglobs.each() { |glob| if(File.fnmatch(glob, basename, File::FNM_DOTMATCH)) exclude = true break end } @excluderegex.each() { |regex| if(regex.match(basename)) exclude = true break end } if(!exclude) return(true) else @includeglobs.each() { |glob| if(File.fnmatch(glob, basename, File::FNM_DOTMATCH)) return(true) end } @includeregex.each() { |regex| if(regex.match(basename)) return(true) end } end return(!exclude) end |