Allow Enumerables to be used with "where" filter.

This commit is contained in:
Mike Kruk 2014-10-08 16:19:55 -04:00
parent cae5958362
commit f6ea8b4d50
2 changed files with 6 additions and 2 deletions

View File

@ -219,7 +219,8 @@ module Jekyll
#
# Returns the filtered array of objects
def where(input, property, value)
return input unless input.is_a?(Array)
return input unless input.is_a?(Enumerable)
input = input.values if input.is_a?(Hash)
input.select { |object| item_property(object, property) == value }
end

View File

@ -172,10 +172,13 @@ class TestFilters < Test::Unit::TestCase
context "where filter" do
should "return any input that is not an array" do
assert_equal Hash.new, @filter.where(Hash.new, nil, nil)
assert_equal "some string", @filter.where("some string", "la", "le")
end
should "filter objects in a hash appropriately" do
assert_equal 1, @filter.where({"a"=>{"color"=>"red"}, "b"=>{"color"=>"blue"}}, "color", "red").length
end
should "filter objects appropriately" do
assert_equal 2, @filter.where(@array_of_objects, "color", "red").length
end