Permalink
Browse files

simplifies PathHelper with a Pathname refinement

  • Loading branch information...
1 parent eda503c commit 8793f77c4a96197bb0f5e29b00828bf5903fcad7 @fxn fxn committed Nov 10, 2015
Showing with 22 additions and 22 deletions.
  1. +22 −22 activesupport/lib/active_support/file_evented_update_checker.rb
@@ -83,6 +83,14 @@ def directories_to_watch
end
class PathHelper
+ using Module.new {
+ refine Pathname do
+ def ascendant_of?(other)
+ other.to_s =~ /\A#{Regexp.quote(to_s)}#{Pathname::SEPARATOR_PAT}?/
+ end
+ end
+ }
+
def xpath(path)
Pathname.new(path).expand_path
end
@@ -96,25 +104,24 @@ def normalize_extension(ext)
def longest_common_subpath(paths)
return if paths.empty?
- csp = Pathname.new(paths[0])
+ lcsp = Pathname.new(paths[0])
paths[1..-1].each do |path|
loop do
- break if path.ascend do |ascendant|
- break true if ascendant == csp
- end
+ break if lcsp.ascendant_of?(path)
- if csp.root?
- # A root directory is not an ascendant of path. This may happen
- # if there are paths in different drives on Windows.
+ if lcsp.root?
+ # If we get here a root directory is not an ascendant of path.
+ # This may happen if there are paths in different drives on
+ # Windows.
return
else
- csp = csp.parent
+ lcsp = lcsp.parent
end
end
end
- csp
+ lcsp
end
# Returns the deepest existing ascendant, which could be the argument itself.
@@ -139,22 +146,15 @@ def existing_parent(dir)
def filter_out_descendants(directories)
return directories if directories.length < 2
- sorted = directories.sort_by {|dir| dir.each_filename.to_a.length}
+ sorted_by_nparts = directories.sort_by {|dir| dir.each_filename.to_a.length}
descendants = []
- until sorted.empty?
- directory = sorted.shift
+ until sorted_by_nparts.empty?
+ dir = sorted_by_nparts.shift
- sorted.each do |candidate_to_descendant|
- if candidate_to_descendant.to_path.start_with?(directory.to_path)
- dparts = directory.each_filename.to_a
- cparts = candidate_to_descendant.each_filename.to_a
-
- if cparts[0, dparts.length] == dparts
- descendants << candidate_to_descendant
- end
- end
- end
+ descendants.concat sorted_by_nparts.select { |possible_descendant|
+ dir.ascendant_of?(possible_descendant)
+ }
end
directories - descendants

0 comments on commit 8793f77

Please sign in to comment.