From f6ea8b4d50217cc91e43a12f3bfb41e223dc8c49 Mon Sep 17 00:00:00 2001 From: Mike Kruk Date: Wed, 8 Oct 2014 16:19:55 -0400 Subject: [PATCH 1/2] Allow Enumerables to be used with "where" filter. --- lib/jekyll/filters.rb | 3 ++- test/test_filters.rb | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index d6f0fa03..cd928891 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -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 diff --git a/test/test_filters.rb b/test/test_filters.rb index 35ea6f0b..84c6e1ae 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -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 From 56ac50c5682cd668fc5c7680c6bf2eff8b2d4b0e Mon Sep 17 00:00:00 2001 From: Mike Kruk Date: Wed, 8 Oct 2014 17:02:36 -0400 Subject: [PATCH 2/2] where filter - test return value when filtering a Hash --- test/test_filters.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test_filters.rb b/test/test_filters.rb index 84c6e1ae..d7457cdd 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -176,7 +176,9 @@ class TestFilters < Test::Unit::TestCase end should "filter objects in a hash appropriately" do - assert_equal 1, @filter.where({"a"=>{"color"=>"red"}, "b"=>{"color"=>"blue"}}, "color", "red").length + hash = {"a"=>{"color"=>"red"}, "b"=>{"color"=>"blue"}} + assert_equal 1, @filter.where(hash, "color", "red").length + assert_equal [{"color"=>"red"}], @filter.where(hash, "color", "red") end should "filter objects appropriately" do