From 39e4d6b3ba9b93f130c99eb665423b4f41ae2cdd Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Sat, 23 Nov 2013 14:40:20 -0500 Subject: [PATCH 1/4] add where filter to filter arrays of objects --- lib/jekyll/filters.rb | 13 ++++++++++++- test/test_filters.rb | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 6ee1b524..67d74a02 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -158,7 +158,6 @@ module Jekyll input.to_json end - # Group an array of items by a property # # input - the inputted Enumerable @@ -179,6 +178,18 @@ module Jekyll end end + # Filter an array of objects + # + # input - the object array + # key - key within each object to filter by + # value - desired value + # + # Returns the filtered array of objects + def where(input, key, value) + return input unless input.class == Array + input.select { |object| object[key] == value } + end + private def time(input) case input diff --git a/test/test_filters.rb b/test/test_filters.rb index 42b0a030..411ceed3 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -16,6 +16,11 @@ class TestFilters < Test::Unit::TestCase @filter = JekyllFilter.new({"source" => source_dir, "destination" => dest_dir}) @sample_time = Time.utc(2013, 03, 27, 11, 22, 33) @time_as_string = "September 11, 2001 12:46:30 -0000" + @array_of_objects = [ + { "color" => "red", "size" => "large" }, + { "color" => "red", "size" => "medium" }, + { "color" => "blue", "size" => "medium" } + ] end should "textilize with simple string" do @@ -109,6 +114,11 @@ class TestFilters < Test::Unit::TestCase assert_equal "[1,2]", @filter.jsonify([1, 2]) assert_equal "[{\"name\":\"Jack\"},{\"name\":\"Smith\"}]", @filter.jsonify([{:name => 'Jack'}, {:name => 'Smith'}]) end + + should "proper filter objects using where" do + assert_equal "some string", @filter.where("some string", nil, nil) + assert_equal 2, @filter.where(@array_of_objects, "color", "red").length + end end context "group_by filter" do From 6ca731c13b93db10d195e9d200496a5e64e688d4 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Thu, 26 Dec 2013 01:28:58 -0500 Subject: [PATCH 2/4] Use idiomatic #is_a? method instead of #class == --- lib/jekyll/filters.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 67d74a02..b3ef7695 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -186,7 +186,7 @@ module Jekyll # # Returns the filtered array of objects def where(input, key, value) - return input unless input.class == Array + return input unless input.is_a?(Array) input.select { |object| object[key] == value } end From b4383a5c42dd9090812f4b8633973a747b78ec8e Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 31 Dec 2013 19:06:43 -0800 Subject: [PATCH 3/4] Flesh out tests for where filter for @mattr- :smiley: --- test/test_filters.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/test/test_filters.rb b/test/test_filters.rb index 411ceed3..763cc837 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -114,11 +114,6 @@ class TestFilters < Test::Unit::TestCase assert_equal "[1,2]", @filter.jsonify([1, 2]) assert_equal "[{\"name\":\"Jack\"},{\"name\":\"Smith\"}]", @filter.jsonify([{:name => 'Jack'}, {:name => 'Smith'}]) end - - should "proper filter objects using where" do - assert_equal "some string", @filter.where("some string", nil, nil) - assert_equal 2, @filter.where(@array_of_objects, "color", "red").length - end end context "group_by filter" do @@ -141,5 +136,17 @@ class TestFilters < Test::Unit::TestCase end end end + + context "where filter" do + should "return the object if it's not an array" do + assert_equal {}, @filter.where({}, nil, nil) + assert_equal "some string", @filter.where("some string", "la", "le") + end + + should "properly filter the objects" do + assert_equal 2, @filter.where(@array_of_objects, "color", "red").length + end + end + end end From a5f1bc034194dd39241d2308a558620a55631cf3 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 3 Jan 2014 21:22:55 -0800 Subject: [PATCH 4/4] Fixed the tests... I guess Hash literals aren't ok. --- test/test_filters.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_filters.rb b/test/test_filters.rb index 763cc837..0d1da02f 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -138,12 +138,12 @@ class TestFilters < Test::Unit::TestCase end context "where filter" do - should "return the object if it's not an array" do - assert_equal {}, @filter.where({}, nil, nil) + 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 "properly filter the objects" do + should "filter objects appropriately" do assert_equal 2, @filter.where(@array_of_objects, "color", "red").length end end