Class: Cfruby::FileFind::FileMatch

Inherits:
Object
  • Object
show all
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

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(options = {})
  @globs = Array.new()
  @regex = Array.new()
  if(options[:glob])
    if(options[:glob].kind_of?(String))
      Cfruby.controller.inform('debug', "adding \"#{options[:glob].to_s}\" to search parameters")
      @globs << options[:glob]
    elsif(options[:glob].kind_of?(Regexp))
      Cfruby.controller.inform('debug', "adding \"#{options[:glob].to_s}\" to search parameters as a regular expression")
      @regex << options[:glob]
    elsif(options[:glob][0].kind_of?(String))
      options[:glob].each() { |subglob|
        Cfruby.controller.inform('debug', "adding \"#{subglob.to_s}\" to search parameters")
        @globs << subglob
      }
    elsif(options[:glob][0].kind_of?(Regexp))
      options[: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(options[:directoriesonly])
    Cfruby.controller.inform('debug', "limiting search to directories only")
    @matchmethods << :directoriesonly
  elsif(options[:filesonly])
    Cfruby.controller.inform('debug', "limiting search to files only")
    @matchmethods << :filesonly
  end

  if(options[:olderthan])
    if(!options[:olderthan].kind_of?(Time))
      @olderthan = Time.parse(options[:olderthan])
    else
      @olderthan = options[:olderthan]
    end
    Cfruby.controller.inform('debug', "limiting search to items older than #{@olderthan.to_s}")
    @matchmethods << :olderthan
  end
  
  if(options[:smallerthan])
    @smallerthan = convertsize(options[:smallerthan])
    Cfruby.controller.inform('debug', "limiting search to items smaller than #{@smallerthan} bytes")
    @matchmethods << :smallerthan
  end

  if(options[:largerthan])
    @largerthan = convertsize(options[:largerthan])
    Cfruby.controller.inform('debug', "limiting search to items larger than #{@largerthan} bytes")
    @matchmethods << :largerthan
  end
  
  if(options[:empty])
    @matchmethods << :empty
  end
  
  if(options[:brokenlink])
    @matchmethods << :brokenlink
  end

  @excludeglobs = Array.new()
  @excluderegex = Array.new()
  @includeglobs = Array.new()
  @includeregex = Array.new()
  if(options[:exclude])
    if(options[:exclude].kind_of?(String))
      Cfruby.controller.inform('debug', "excluding items matching #{options[:exclude]}")
      @excludeglobs << options[:exclude]
    elsif(options[:exclude].kind_of?(Regexp))
      Cfruby.controller.inform('debug', "excluding items matching #{options[:exclude]} as a regular expression")
      @excluderegex << options[:exclude]
    elsif(options[:exclude][0].kind_of?(String))
      options[:exclude].each() { |subglob|
        Cfruby.controller.inform('debug', "excluding items matching #{subglob} as a regular expression")
        @excludeglobs << subglob
      }
    elsif(options[:exclude][0].kind_of?(Regexp))
      options[:exclude].each() { |subglob|
        Cfruby.controller.inform('debug', "excluding items matching #{subglob}")
        @excluderegex << subglob
      }
    end
  end
  
  if(options[:include])
    if(options[:include].kind_of?(String))
      Cfruby.controller.inform('debug', "including items matching #{options[:include]}")
      @includeglobs << options[:include]
    elsif(options[:include].kind_of?(Regexp))
      Cfruby.controller.inform('debug', "including items matching #{options[:include]} as a regular expression")
      @includeregex << options[:include]
    elsif(options[:include][0].kind_of?(String))
      options[:include].each() { |subglob|
        Cfruby.controller.inform('debug', "including items matching #{subglob}")
        @includeglobs << subglob
      }
    elsif(options[:include][0].kind_of?(Regexp))
      options[: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

Returns:

  • (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