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
No known key found for this signature in database
GPG Key ID: F1746FF5F18B3D1B
2 changed files with 18 additions and 4 deletions

View File

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

View File

@ -15,6 +15,10 @@ class TestFilters < JekyllUnitTest
end end
end end
class SelectDummy
def select; end
end
context "filters" do context "filters" do
setup do setup do
@filter = JekyllFilter.new({ @filter = JekyllFilter.new({
@ -530,6 +534,11 @@ class TestFilters < JekyllUnitTest
assert_equal 1, results.length assert_equal 1, results.length
assert_equal 4.7, results[0]["rating"] assert_equal 4.7, results[0]["rating"]
end 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 end
context "where_exp filter" do context "where_exp filter" do
@ -602,6 +611,11 @@ class TestFilters < JekyllUnitTest
assert_equal 1, results.length assert_equal 1, results.length
assert_equal site.posts.find { |p| p.title == "Foo Bar" }, results.first assert_equal site.posts.find { |p| p.title == "Foo Bar" }, results.first
end 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 end
context "sort filter" do context "sort filter" do