Skip to content

Commit 8793f77

Browse files
committed
simplifies PathHelper with a Pathname refinement
1 parent eda503c commit 8793f77

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

activesupport/lib/active_support/file_evented_update_checker.rb

+22-22
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ def directories_to_watch
8383
end
8484

8585
class PathHelper
86+
using Module.new {
87+
refine Pathname do
88+
def ascendant_of?(other)
89+
other.to_s =~ /\A#{Regexp.quote(to_s)}#{Pathname::SEPARATOR_PAT}?/
90+
end
91+
end
92+
}
93+
8694
def xpath(path)
8795
Pathname.new(path).expand_path
8896
end
@@ -96,25 +104,24 @@ def normalize_extension(ext)
96104
def longest_common_subpath(paths)
97105
return if paths.empty?
98106

99-
csp = Pathname.new(paths[0])
107+
lcsp = Pathname.new(paths[0])
100108

101109
paths[1..-1].each do |path|
102110
loop do
103-
break if path.ascend do |ascendant|
104-
break true if ascendant == csp
105-
end
111+
break if lcsp.ascendant_of?(path)
106112

107-
if csp.root?
108-
# A root directory is not an ascendant of path. This may happen
109-
# if there are paths in different drives on Windows.
113+
if lcsp.root?
114+
# If we get here a root directory is not an ascendant of path.
115+
# This may happen if there are paths in different drives on
116+
# Windows.
110117
return
111118
else
112-
csp = csp.parent
119+
lcsp = lcsp.parent
113120
end
114121
end
115122
end
116123

117-
csp
124+
lcsp
118125
end
119126

120127
# Returns the deepest existing ascendant, which could be the argument itself.
@@ -139,22 +146,15 @@ def existing_parent(dir)
139146
def filter_out_descendants(directories)
140147
return directories if directories.length < 2
141148

142-
sorted = directories.sort_by {|dir| dir.each_filename.to_a.length}
149+
sorted_by_nparts = directories.sort_by {|dir| dir.each_filename.to_a.length}
143150
descendants = []
144151

145-
until sorted.empty?
146-
directory = sorted.shift
152+
until sorted_by_nparts.empty?
153+
dir = sorted_by_nparts.shift
147154

148-
sorted.each do |candidate_to_descendant|
149-
if candidate_to_descendant.to_path.start_with?(directory.to_path)
150-
dparts = directory.each_filename.to_a
151-
cparts = candidate_to_descendant.each_filename.to_a
152-
153-
if cparts[0, dparts.length] == dparts
154-
descendants << candidate_to_descendant
155-
end
156-
end
157-
end
155+
descendants.concat sorted_by_nparts.select { |possible_descendant|
156+
dir.ascendant_of?(possible_descendant)
157+
}
158158
end
159159

160160
directories - descendants

0 commit comments

Comments
 (0)