object needs to respond to select instead of being an enumerable

This commit is contained in:
Florian Thomas
2016-07-10 20:52:39 +02:00
committed by Pat Hawks
parent 2b57795c7f
commit 99663a9199
2 changed files with 18 additions and 4 deletions

View File

@@ -233,11 +233,11 @@ module Jekyll
#
# Returns the filtered array of objects
def where(input, property, value)
return input unless input.is_a?(Enumerable)
return input unless input.respond_to?(:select)
input = input.values if input.is_a?(Hash)
input.select do |object|
Array(item_property(object, property)).map(&:to_s).include?(value.to_s)
end
end || []
end
# Filters an array of objects against an expression
@@ -248,7 +248,7 @@ module Jekyll
#
# Returns the filtered array of objects
def where_exp(input, variable, expression)
return input unless input.is_a?(Enumerable)
return input unless input.respond_to?(:select)
input = input.values if input.is_a?(Hash) # FIXME
condition = parse_condition(expression)
@@ -257,7 +257,7 @@ module Jekyll
@context[variable] = object
condition.evaluate(@context)
end
end
end || []
end
# Sort an array of objects

View File

@@ -15,6 +15,10 @@ class TestFilters < JekyllUnitTest
end
end
class SelectDummy
def select; end
end
context "filters" do
setup do
@filter = JekyllFilter.new({
@@ -530,6 +534,11 @@ class TestFilters < JekyllUnitTest
assert_equal 1, results.length
assert_equal 4.7, results[0]["rating"]
end
should "always return an array if the object responds to `select`" do
results = @filter.where(SelectDummy.new, "obj", "1 == 1")
assert_equal [], results
end
end
context "where_exp filter" do
@@ -602,6 +611,11 @@ class TestFilters < JekyllUnitTest
assert_equal 1, results.length
assert_equal site.posts.find { |p| p.title == "Foo Bar" }, results.first
end
should "always return an array if the object responds to `select`" do
results = @filter.where_exp(SelectDummy.new, "obj", "1 == 1")
assert_equal [], results
end
end
context "sort filter" do