Class: PathList
Overview
A PathList is essentially an array with a few helper methods defined to make file manipulation a bit easier.
PathLists are lazy. When given a list of glob patterns for possible files to be included in the file list, instead of searching the file structures to find the files, a PathList holds the pattern for latter use.
This allows us to define a number of PathList to match any number of files, but only search out the actual files when then PathList itself is actually used. The key is that the first time an element of the PathList/Array is requested, the pending patterns are resolved into a real list of file names.
Defined Under Namespace
Classes: PathAndMatchData
Constant Summary collapse
- MUST_DEFINE =
List of additional methods that must be delegated.
%w[to_a inspect]
- MUST_NOT_DEFINE =
List of methods that should not be delegated here (we define special versions of them explicitly below).
%w[to_a to_ary partition *]
- SPECIAL_RETURN =
List of delegated methods that return new array values which need wrapping.
%w[ map collect sort sort_by select find_all reject grep compact flatten uniq values_at + - & | ]
- DELEGATING_METHODS =
(ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).sort.uniq
Class Method Summary collapse
-
.[](*args) ⇒ Object
Create a new file list including the files listed.
-
.clear_ignore_patterns ⇒ Object
Clear the ignore patterns.
- .import(array) ⇒ Object
-
.select_default_ignore_patterns ⇒ Object
Set the ignore patterns back to the default value.
- .yaml_load(val) ⇒ Object
Instance Method Summary collapse
-
#*(other) ⇒ Object
Redefine * to return either a string or a new file list.
-
#==(array) ⇒ Object
Define equality.
- #calculate_exclude_regexp ⇒ Object
-
#clear_exclude ⇒ Object
Clear all the exclude patterns so that we exclude nothing.
- #clone ⇒ Object (also: #dup)
- #each(&block) ⇒ 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?.
-
#ext(newext = '') ⇒ Object
Return a new array with
Pathname#ext
method applied to each member of the array. -
#gsub(*args, &block) ⇒ Object
Return a new PathList with the results of running
gsub
against each element of the original list. -
#gsub!(*args, &block) ⇒ Object
Same as
gsub
except that the original file list is modified. - #import(array) ⇒ Object
- #import! ⇒ Object
-
#include(*filenames) ⇒ Object
(also: #add)
Add file names defined by glob patterns to the file list.
-
#initialize(*patterns) {|_self| ... } ⇒ PathList
constructor
Create a file list from the globbable patterns given.
- #load_path! ⇒ Object
-
#partition(&block) ⇒ Object
PathList version of partition.
-
#resolve ⇒ Object
Resolve all the pending adds now.
- #resolve_add(fn) ⇒ Object
- #resolve_exclude ⇒ Object
- #stringify ⇒ Object
-
#sub(*args, &block) ⇒ Object
Return a new PathList with the results of running
sub
against each element of the oringal list. -
#sub!(*args, &block) ⇒ Object
Same as
sub
except that the oringal file list is modified. -
#to_a ⇒ Object
Return the internal array object.
-
#to_ary ⇒ Object
Return the internal array object.
-
#to_s ⇒ Object
Convert a PathList to a string by joining all elements with a space.
- #to_yaml(opts = {}) ⇒ Object
-
#traditional_each ⇒ Object
<<<<.
Constructor Details
#initialize(*patterns) {|_self| ... } ⇒ PathList
136 137 138 139 140 141 142 143 144 |
# File 'lib/path_list.rb', line 136 def initialize(*patterns) @pending_add = [] @pending = false @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup @exclude_re = nil @items = [] patterns.each { |pattern| include(pattern) } yield self if block_given? end |
Class Method Details
.[](*args) ⇒ Object
440 441 442 |
# File 'lib/path_list.rb', line 440 def [](*args) new(*args) end |
.clear_ignore_patterns ⇒ Object
Clear the ignore patterns.
464 465 466 |
# File 'lib/path_list.rb', line 464 def clear_ignore_patterns @exclude_patterns = [ /^$/ ] end |
.import(array) ⇒ Object
444 445 446 |
# File 'lib/path_list.rb', line 444 def import(array) new.import(array) 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
-
containing “.svn” 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.
459 460 461 |
# File 'lib/path_list.rb', line 459 def select_default_ignore_patterns @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup end |
.yaml_load(val) ⇒ Object
362 363 364 |
# File 'lib/path_list.rb', line 362 def self.yaml_load ( val ) new(val) end |
Instance Method Details
#*(other) ⇒ Object
Redefine * to return either a string or a new file list.
222 223 224 225 226 227 228 229 230 |
# File 'lib/path_list.rb', line 222 def *(other) result = @items * other case result when Array PathList.import result else result end end |
#==(array) ⇒ Object
Define equality.
205 206 207 |
# File 'lib/path_list.rb', line 205 def ==(array) to_ary == array end |
#calculate_exclude_regexp ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/path_list.rb', line 243 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
Clear all the exclude patterns so that we exclude nothing.
199 200 201 202 |
# File 'lib/path_list.rb', line 199 def clear_exclude @exclude_patterns = [] calculate_exclude_regexp if ! @pending end |
#clone ⇒ Object Also known as: dup
51 52 53 54 55 56 57 58 |
# File 'lib/path_list.rb', line 51 def clone sibling = self.class.new instance_variables.each do |ivar| value = self.instance_variable_get(ivar) sibling.instance_variable_set(ivar, value.try_dup) end sibling end |
#each(&block) ⇒ Object
471 472 473 474 475 476 477 478 479 |
# File 'lib/path_list.rb', line 471 def each ( &block ) traditional_each do |x| if x.is_a? PathAndMatchData block[x, *x.match.to_a[1..-1]] else block[x] end end 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:
PathList['a.c', 'b.c'].exclude("a.c") => ['b.c']
PathList['a.c', 'b.c'].exclude(/^a/) => ['b.c']
If “a.c” is a file, then …
PathList['a.c', 'b.c'].exclude("a.*") => ['b.c']
If “a.c” is not a file, then …
PathList['a.c', 'b.c'].exclude("a.*") => ['a.c', 'b.c']
186 187 188 189 190 191 192 193 |
# File 'lib/path_list.rb', line 186 def exclude(*patterns) patterns.each do |pat| @exclude_patterns << pat end if ! @pending calculate_exclude_regexp reject! { |fn| fn.to_s =~ @exclude_re } end self end |
#exclude?(fn) ⇒ Boolean
Should the given file name be excluded?
414 415 416 417 418 419 |
# File 'lib/path_list.rb', line 414 def exclude?(fn) calculate_exclude_regexp unless @exclude_re # <<<< fn.to_s =~ @exclude_re # >>>> end |
#ext(newext = '') ⇒ Object
Return a new array with Pathname#ext
method applied to each member of the array.
This method is a shortcut for:
array.collect { |item| item.ext(newext) }
ext
is a user added method for the Array class.
338 339 340 |
# File 'lib/path_list.rb', line 338 def ext(newext='') collect { |fn| fn.ext(newext) } end |
#gsub(*args, &block) ⇒ Object
Return a new PathList with the results of running gsub
against each element of the original list.
Example:
PathList['lib/test/file', 'x/y'].gsub(/\//, "\\")
=> ['lib\\test\\file', 'x\\y']
314 315 316 |
# File 'lib/path_list.rb', line 314 def gsub(*args, &block) inject(PathList.new) { |res, fn| res << fn.gsub(*args, &block) } end |
#gsub!(*args, &block) ⇒ Object
Same as gsub
except that the original file list is modified.
325 326 327 328 |
# File 'lib/path_list.rb', line 325 def gsub!(*args, &block) each_with_index { |fn, i| self[i] = fn.gsub(*args, &block) } self end |
#import(array) ⇒ Object
431 432 433 434 |
# File 'lib/path_list.rb', line 431 def import(array) @items = array self end |
#import! ⇒ Object
481 482 483 |
# File 'lib/path_list.rb', line 481 def import! traditional_each { |x| x.import! } 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 )
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/path_list.rb', line 153 def include(*filenames) # TODO: check for pending filenames.each do |fn| if fn.respond_to? :to_ary include(*fn.to_ary) else @pending_add << fn end end @pending = true self end |
#load_path! ⇒ Object
485 486 487 |
# File 'lib/path_list.rb', line 485 def load_path! traditional_each { |x| x.load_path! } end |
#partition(&block) ⇒ Object
PathList version of partition. Needed because the nested arrays should be PathLists in this version.
345 346 347 348 349 350 351 352 |
# File 'lib/path_list.rb', line 345 def partition(&block) # :nodoc: resolve result = @items.partition(&block) [ PathList.import(result[0]), PathList.import(result[1]), ] end |
#resolve ⇒ Object
Resolve all the pending adds now.
233 234 235 236 237 238 239 240 241 |
# File 'lib/path_list.rb', line 233 def resolve if @pending @pending = false @pending_add.each do |fn| resolve_add(fn) end @pending_add = [] resolve_exclude end self end |
#resolve_add(fn) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/path_list.rb', line 263 def resolve_add(fn) case fn when Array fn.each { |f| self.resolve_add(f) } when %r{[*?\{]} add_matching(fn) else # >>>> self << fn.to_path # <<<< end end |
#resolve_exclude ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/path_list.rb', line 276 def resolve_exclude @exclude_patterns.each do |pat| case pat when Regexp # <<<< reject! { |fn| fn.to_s =~ pat } # >>>> when /[*.]/ # <<<< reject_list = Pathname.glob(pat) # >>>> reject! { |fn| reject_list.include?(fn) } else # <<<< reject! { |fn| fn.to_s == pat.to_s } # >>>> end end self end |
#stringify ⇒ Object
489 490 491 |
# File 'lib/path_list.rb', line 489 def stringify map { |path| path.to_s } end |
#sub(*args, &block) ⇒ Object
Return a new PathList with the results of running sub
against each element of the oringal list.
Example:
PathList['a.c', 'b.c'].sub(/\.c$/, '.o') => ['a.o', 'b.o']
303 304 305 |
# File 'lib/path_list.rb', line 303 def sub(*args, &block) inject(PathList.new) { |res, fn| res << fn.sub(*args, &block) } end |
#sub!(*args, &block) ⇒ Object
Same as sub
except that the oringal file list is modified.
319 320 321 322 |
# File 'lib/path_list.rb', line 319 def sub!(*args, &block) each_with_index { |fn, i| self[i] = fn.sub(*args, &block) } self end |
#to_a ⇒ Object
Return the internal array object.
210 211 212 213 |
# File 'lib/path_list.rb', line 210 def to_a resolve @items end |
#to_ary ⇒ Object
Return the internal array object.
216 217 218 219 |
# File 'lib/path_list.rb', line 216 def to_ary resolve @items end |
#to_s ⇒ Object
Convert a PathList to a string by joining all elements with a space.
355 356 357 358 |
# File 'lib/path_list.rb', line 355 def to_s resolve if @pending self.join(' ') end |
#to_yaml(opts = {}) ⇒ Object
366 367 368 369 |
# File 'lib/path_list.rb', line 366 def to_yaml ( opts={} ) resolve if @pending to_a.to_yaml(opts) end |
#traditional_each ⇒ Object
<<<<
470 |
# File 'lib/path_list.rb', line 470 alias_method :traditional_each, :each |