Class: FileList
Overview
:title: FileList
A FileList is essentially an array with a few helper methods defined to make file manipulation a bit easier. It works in a fashion similar to Dir.glob.
Synopsis
require 'mega/filelist'
fl = FileList.new
fl.include('./**/*')
fl.exclude('./*~')
Constant Summary collapse
- DEFAULT_IGNORE_PATTERNS =
[ /(^|[\/\\])CVS([\/\\]|$)/, /\.bak$/, /~$/, /(^|[\/\\])core$/ ]
Class Method Summary collapse
-
.[](*args) ⇒ Object
Create a new file list including the files listed.
-
.clear_ignore_patterns ⇒ Object
Clear the ignore patterns.
-
.select_default_ignore_patterns ⇒ Object
Set the ignore patterns back to the default value.
Instance Method Summary collapse
- #calculate_exclude_regexp ⇒ Object
- #clear_exclude ⇒ Object
-
#exclude(*patterns) ⇒ Object
Register a list of file name patterns that should be excluded from the list.
-
#exclude?(fn) ⇒ Boolean
Should the given file name be excluded?.
-
#gsub(pat, rep) ⇒ Object
Return a new FileList with the results of running
gsub
against each element of the original list. -
#gsub!(pat, rep) ⇒ Object
Same as
gsub
except that the original file list is modified. -
#include(*filenames) ⇒ Object
(also: #add)
Add file names defined by glob patterns to the file list.
-
#initialize(*patterns) {|_self| ... } ⇒ FileList
constructor
Create a file list from the globbable patterns given.
-
#resolve ⇒ Object
Resolve all the pending adds now.
- #resolve_add(fn) ⇒ Object
- #resolve_exclude ⇒ Object
-
#sub(pat, rep) ⇒ Object
Return a new FileList with the results of running
sub
against each element of the oringal list. -
#sub!(pat, rep) ⇒ Object
Same as
sub
except that the oringal file list is modified. -
#to_s ⇒ Object
Convert a FileList to a string by joining all elements with a space.
-
#to_str ⇒ Object
Convert a FileList to a string prior to resolving.
Methods included from DupReplaceSnapshotMixin
#restore_snapshot, #take_snapshot
Constructor Details
#initialize(*patterns) {|_self| ... } ⇒ FileList
65 66 67 68 69 70 71 72 73 |
# File 'lib/mega/filelist.rb', line 65 def initialize(*patterns) @pending_add = [] @pending = false @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup @exclude_re = nil # @extent_only = false patterns.each { |pattern| include(pattern) } yield self if block_given? end |
Class Method Details
.[](*args) ⇒ Object
260 261 262 |
# File 'lib/mega/filelist.rb', line 260 def [](*args) new(*args) end |
.clear_ignore_patterns ⇒ Object
Clear the ignore patterns.
279 280 281 |
# File 'lib/mega/filelist.rb', line 279 def clear_ignore_patterns @exclude_patterns = [ /^$/ ] end |
.select_default_ignore_patterns ⇒ Object
Set the ignore patterns back to the default value. The default patterns will ignore files
-
containing “CVS” in the file path
-
ending with “.bak”
-
ending with “~”
-
named “core”
Note that file names beginning with “.” are automatically ignored by Ruby’s glob patterns and are not specifically listed in the ignore patterns.
274 275 276 |
# File 'lib/mega/filelist.rb', line 274 def select_default_ignore_patterns @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup end |
Instance Method Details
#calculate_exclude_regexp ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/mega/filelist.rb', line 142 def calculate_exclude_regexp ignores = [] @exclude_patterns.each do |pat| case pat when Regexp ignores << pat when /[*.]/ Dir[pat].each do |p| ignores << p end else ignores << Regexp.quote(pat) end end if ignores.empty? @exclude_re = /^$/ else re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|") @exclude_re = Regexp.new(re_str) end end |
#clear_exclude ⇒ Object
128 129 130 131 |
# File 'lib/mega/filelist.rb', line 128 def clear_exclude @exclude_patterns = [] calculate_exclude_regexp if ! @pending end |
#exclude(*patterns) ⇒ Object
Register a list of file name patterns that should be excluded from the list. Patterns may be regular expressions, glob patterns or regular strings.
Note that glob patterns are expanded against the file system. If a file is explicitly added to a file list, but does not exist in the file system, then an glob pattern in the exclude list will not exclude the file.
Examples:
FileList['a.c', 'b.c'].exclude("a.c") => ['b.c']
FileList['a.c', 'b.c'].exclude(/^a/) => ['b.c']
If “a.c” is a file, then …
FileList['a.c', 'b.c'].exclude("a.*") => ['b.c']
If “a.c” is not a file, then …
FileList['a.c', 'b.c'].exclude("a.*") => ['a.c', 'b.c']
118 119 120 121 122 123 124 125 |
# File 'lib/mega/filelist.rb', line 118 def exclude(*patterns) # TODO: check for pending patterns.compact.each { |pat| @exclude_patterns << pat } if ! @pending calculate_exclude_regexp reject! { |fn| fn =~ @exclude_re } end end |
#exclude?(fn) ⇒ Boolean
Should the given file name be excluded?
243 244 245 246 |
# File 'lib/mega/filelist.rb', line 243 def exclude?(fn) calculate_exclude_regexp unless @exclude_re fn =~ @exclude_re end |
#gsub(pat, rep) ⇒ Object
Return a new FileList with the results of running gsub
against each element of the original list.
Example:
FileList['lib/test/file', 'x/y'].gsub(/\//, "\\")
=> ['lib\\test\\file', 'x\\y']
207 208 209 |
# File 'lib/mega/filelist.rb', line 207 def gsub(pat, rep) inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) } end |
#gsub!(pat, rep) ⇒ Object
Same as gsub
except that the original file list is modified.
218 219 220 221 |
# File 'lib/mega/filelist.rb', line 218 def gsub!(pat, rep) each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) } self end |
#include(*filenames) ⇒ Object Also known as: add
Add file names defined by glob patterns to the file list. If an array is given, add each element of the array.
Example:
file_list.include("*.java", "*.cfg")
file_list.include %w( math.c lib.h *.o )
82 83 84 85 86 87 |
# File 'lib/mega/filelist.rb', line 82 def include(*filenames) # TODO: check for pending filenames.each do |fn| @pending_add << fn end @pending = true self end |
#resolve ⇒ Object
Resolve all the pending adds now.
134 135 136 137 138 139 140 |
# File 'lib/mega/filelist.rb', line 134 def resolve @pending = false @pending_add.each do |fn| resolve_add(fn) end @pending_add = [] resolve_exclude self end |
#resolve_add(fn) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/mega/filelist.rb', line 162 def resolve_add(fn) case fn when NilClass # ignore when Array fn.each { |f| self.resolve_add(f) } when %r{[*?]} add_matching(fn) else self << fn if FileTest.exists?( fn ) end end |
#resolve_exclude ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/mega/filelist.rb', line 175 def resolve_exclude @exclude_patterns.each do |pat| case pat when Regexp reject! { |fn| fn =~ pat } when /[*.]/ reject_list = Dir[pat] reject! { |fn| reject_list.include?(fn) } else reject! { |fn| fn == pat } end end self end |
#sub(pat, rep) ⇒ Object
Return a new FileList with the results of running sub
against each element of the oringal list.
Example:
FileList['a.c', 'b.c'].sub(/\.c$/, '.o') => ['a.o', 'b.o']
196 197 198 |
# File 'lib/mega/filelist.rb', line 196 def sub(pat, rep) inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) } end |
#sub!(pat, rep) ⇒ Object
Same as sub
except that the oringal file list is modified.
212 213 214 215 |
# File 'lib/mega/filelist.rb', line 212 def sub!(pat, rep) each_with_index { |fn, i| self[i] = fn.sub(pat,rep) } self end |
#to_s ⇒ Object
Convert a FileList to a string by joining all elements with a space.
224 225 226 227 |
# File 'lib/mega/filelist.rb', line 224 def to_s resolve self.join(' ') end |
#to_str ⇒ Object
Convert a FileList to a string prior to resolving.
230 231 232 |
# File 'lib/mega/filelist.rb', line 230 def to_str self.join(' ') end |