From 229303bbc8cea179737403fc8465db3c21100879 Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Thu, 27 Nov 2014 04:01:25 -0600 Subject: [PATCH 1/4] compare resulting data in jsonify test --- test/test_filters.rb | 66 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/test/test_filters.rb b/test/test_filters.rb index d46d9352..a9fc951d 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -160,14 +160,70 @@ class TestFilters < Test::Unit::TestCase end should "call #to_liquid " do - expected = "[{\"name\":\"Jeremiah\",\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}],\"stuff\":true},{\"name\":\"Smathers\",\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}],\"stuff\":true}]" - assert_equal expected, @filter.jsonify([T.new("Jeremiah"), T.new("Smathers")]) + expected = [ + { + "name" => "Jeremiah", + "v" => 1, + "thing" => [ + { + "kay" => "jewelers" + } + ], + "stuff" => true + }, + { + "name" => "Smathers", + "v" => 1, + "thing" => [ + { + "kay" => "jewelers" + } + ], + "stuff" => true + } + ] + result = @filter.jsonify([T.new("Jeremiah"), T.new("Smathers")]) + assert_equal expected, JSON.parse(result) end should "handle hashes with all sorts of weird keys and values" do - my_hash = { "posts" => Array.new(5) { |i| T.new(i) } } - expected = "{\"posts\":[{\"name\":0,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}],\"stuff\":true},{\"name\":1,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}],\"stuff\":true},{\"name\":2,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}],\"stuff\":true},{\"name\":3,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}],\"stuff\":true},{\"name\":4,\"v\":1,\"thing\":[{\"kay\":\"jewelers\"}],\"stuff\":true}]}" - assert_equal expected, @filter.jsonify(my_hash) + my_hash = { "posts" => Array.new(3) { |i| T.new(i) } } + expected = { + "posts" => [ + { + "name" => 0, + "v" => 1, + "thing" => [ + { + "kay" => "jewelers" + } + ], + "stuff" => true + }, + { + "name" => 1, + "v" => 1, + "thing" => [ + { + "kay" => "jewelers" + } + ], + "stuff" => true + }, + { + "name" => 2, + "v" => 1, + "thing" => [ + { + "kay" => "jewelers" + } + ], + "stuff" => true + } + ] + } + result = @filter.jsonify(my_hash) + assert_equal expected, JSON.parse(result) end end From 87d08ec827f035f5d57cb86818a0280962b22846 Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Thu, 27 Nov 2014 04:16:29 -0600 Subject: [PATCH 2/4] refactor #as_liquid --- lib/jekyll/filters.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 2e4a1174..fc0907a9 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -328,14 +328,23 @@ module Jekyll def as_liquid(item) case item - when String, Numeric, true, false, nil - item.to_liquid when Hash - Hash[item.map { |k, v| [as_liquid(k), as_liquid(v)] }] + pairs = item.map { |k, v| as_liquid([k, v]) } + Hash[pairs] when Array item.map{ |i| as_liquid(i) } - else - item.respond_to?(:to_liquid) ? as_liquid(item.to_liquid) : item + else # simple type + if item.respond_to?(:to_liquid) + liquidated = item.to_liquid + # prevent infinite recursion + if liquidated == item + item + else + as_liquid(liquidated) + end + else + item + end end end end From 4776b27ff363fe2e7406be77f5f2f061ee8ecb8f Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Thu, 27 Nov 2014 04:20:05 -0600 Subject: [PATCH 3/4] reduce nesting of #as_liquid --- lib/jekyll/filters.rb | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index fc0907a9..e147c93e 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -327,24 +327,21 @@ module Jekyll end def as_liquid(item) - case item - when Hash + if item.class == Hash pairs = item.map { |k, v| as_liquid([k, v]) } Hash[pairs] - when Array + elsif item.class == Array item.map{ |i| as_liquid(i) } - else # simple type - if item.respond_to?(:to_liquid) - liquidated = item.to_liquid - # prevent infinite recursion - if liquidated == item - item - else - as_liquid(liquidated) - end - else + elsif item.respond_to?(:to_liquid) + liquidated = item.to_liquid + # prevent infinite recursion for simple types (which return `self`) + if liquidated == item item + else + as_liquid(liquidated) end + else + item end end end From 383a0d0aa79405605ffb4638e85249778f6232a7 Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Tue, 9 Dec 2014 07:42:00 -0500 Subject: [PATCH 4/4] modify as_liquid to use case statement --- lib/jekyll/filters.rb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index e147c93e..f92c02b1 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -327,21 +327,24 @@ module Jekyll end def as_liquid(item) - if item.class == Hash + case item + when Hash pairs = item.map { |k, v| as_liquid([k, v]) } Hash[pairs] - elsif item.class == Array + when Array item.map{ |i| as_liquid(i) } - elsif item.respond_to?(:to_liquid) - liquidated = item.to_liquid - # prevent infinite recursion for simple types (which return `self`) - if liquidated == item - item - else - as_liquid(liquidated) - end else - item + if item.respond_to?(:to_liquid) + liquidated = item.to_liquid + # prevent infinite recursion for simple types (which return `self`) + if liquidated == item + item + else + as_liquid(liquidated) + end + else + item + end end end end