add Enumerable#glob_include?

This commit is contained in:
mccxj 2013-01-11 15:33:11 +08:00
parent b3e27f2c5d
commit 3cf29eeee2
3 changed files with 32 additions and 6 deletions

View File

@ -50,3 +50,11 @@ class Date
strftime("%Y-%m-%dT%H:%M:%S%Z") strftime("%Y-%m-%dT%H:%M:%S%Z")
end if RUBY_VERSION < '1.9' end if RUBY_VERSION < '1.9'
end end
module Enumerable
# Returns true if path matches against any glob pattern.
# Look for more detail about glob pattern in method File::fnmatch.
def glob_include?(e)
any? { |exp| File.fnmatch?(exp, e) }
end
end

View File

@ -318,19 +318,15 @@ module Jekyll
# Returns the Array of filtered entries. # Returns the Array of filtered entries.
def filter_entries(entries) def filter_entries(entries)
entries.reject do |e| entries.reject do |e|
unless glob_include?(self.include, e) unless self.include.glob_include?(e)
['.', '_', '#'].include?(e[0..0]) || ['.', '_', '#'].include?(e[0..0]) ||
e[-1..-1] == '~' || e[-1..-1] == '~' ||
glob_include?(self.exclude, e) || self.exclude.glob_include?(e) ||
File.symlink?(e) File.symlink?(e)
end end
end end
end end
def glob_include?(exps, e)
exps.any? { |exp| File.fnmatch?(exp, e) }
end
# Get the implementation class for the given Converter. # Get the implementation class for the given Converter.
# #
# klass - The Class of the Converter to fetch. # klass - The Class of the Converter to fetch.

View File

@ -63,4 +63,26 @@ class TestCoreExt < Test::Unit::TestCase
end end
end end
context "enumerable" do
context "glob_include?" do
should "return false with no glob patterns" do
assert ![].glob_include?("a.txt")
end
should "return false with all not match path" do
data = ["a*", "b?"]
assert !data.glob_include?("ca.txt")
assert !data.glob_include?("ba.txt")
end
should "return true with match path" do
data = ["a*", "b?", "**/a*"]
assert data.glob_include?("a.txt")
assert data.glob_include?("ba")
assert data.glob_include?("c/a/a.txt")
assert data.glob_include?("c/a/b/a.txt")
end
end
end
end end