From 96bc62c666b598c3dc7df7e2ec8a9b22625ed0af Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 4 Dec 2015 09:33:33 -0800 Subject: [PATCH 1/5] Add 'sample' Liquid filter Equivalent to Array#sample functionality --- lib/jekyll/filters.rb | 5 +++++ site/_docs/templates.md | 11 +++++++++++ test/test_filters.rb | 7 +++++++ 3 files changed, 23 insertions(+) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 7e2d30f3..f3193f21 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -281,6 +281,11 @@ module Jekyll new_ary end + def sample(input) + return input unless input.respond_to?(:sample) + input.sample(1) + end + # Convert an object into its String representation for debugging # # input - The Object to be converted diff --git a/site/_docs/templates.md b/site/_docs/templates.md index 58e06c94..20a3a3f2 100644 --- a/site/_docs/templates.md +++ b/site/_docs/templates.md @@ -246,6 +246,17 @@ common tasks easier.

+ + +

Sample

+

Pick a random value from an array.

+ + +

+ {% raw %}{{ site.pages | sample }}{% endraw %} +

+ + diff --git a/test/test_filters.rb b/test/test_filters.rb index cf4db89b..59b882f1 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -388,5 +388,12 @@ class TestFilters < JekyllUnitTest end end + context "sample filter" do + should "return a random item from the array" do + input = %w(hey there bernie) + assert_includes input, @filter.sample(input) + end + end + end end From 86195655d798e4afcb888fa37787453e0d56ae23 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 4 Dec 2015 09:40:57 -0800 Subject: [PATCH 2/5] filters: allow sample(n) instead of just sample(1) --- lib/jekyll/filters.rb | 4 ++-- site/_docs/templates.md | 5 ++++- test/test_filters.rb | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index f3193f21..70e93b90 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -281,9 +281,9 @@ module Jekyll new_ary end - def sample(input) + def sample(input, num = 1) return input unless input.respond_to?(:sample) - input.sample(1) + input.sample(num) end # Convert an object into its String representation for debugging diff --git a/site/_docs/templates.md b/site/_docs/templates.md index 20a3a3f2..fd42b6b1 100644 --- a/site/_docs/templates.md +++ b/site/_docs/templates.md @@ -249,12 +249,15 @@ common tasks easier.

Sample

-

Pick a random value from an array.

+

Pick a random value from an array. Optional: pick multiple values.

{% raw %}{{ site.pages | sample }}{% endraw %}

+

+ {% raw %}{{ site.pages | sample:2 }}{% endraw %} +

diff --git a/test/test_filters.rb b/test/test_filters.rb index 59b882f1..5fd9facb 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -393,6 +393,13 @@ class TestFilters < JekyllUnitTest input = %w(hey there bernie) assert_includes input, @filter.sample(input) end + + should "allow sampling of multiple values (n > 1)" do + input = %w(hey there bernie) + @filter.sample(input, 2).each do |val| + assert_includes val, input + end + end end end From 0aa3c96d1150afe8a69332e5749a799cef89af2f Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 4 Dec 2015 09:59:00 -0800 Subject: [PATCH 3/5] travis: do NOT wait for branch builds for PR's. it just wastes time. --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 8026187c..82e32e86 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,9 @@ env: matrix: - TEST_SUITE=test - TEST_SUITE=cucumber +branches: + only: + - master before_script: bundle update script: script/cibuild notifications: From 2e91d094e5852ccb760010ede2e1062e6aff74a1 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 4 Dec 2015 10:25:13 -0800 Subject: [PATCH 4/5] filters#sample: n == 1, return item; n > 1, return array --- lib/jekyll/filters.rb | 7 ++++++- test/test_filters.rb | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 70e93b90..6efc2e94 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -283,7 +283,12 @@ module Jekyll def sample(input, num = 1) return input unless input.respond_to?(:sample) - input.sample(num) + sampling = input.sample(num) + if num == 1 + sampling.first + else + sampling + end end # Convert an object into its String representation for debugging diff --git a/test/test_filters.rb b/test/test_filters.rb index 5fd9facb..dc51252e 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -397,7 +397,7 @@ class TestFilters < JekyllUnitTest should "allow sampling of multiple values (n > 1)" do input = %w(hey there bernie) @filter.sample(input, 2).each do |val| - assert_includes val, input + assert_includes input, val end end end From 47d2a2459d0dbe07301f8d8fb5bbf142bf5eb2d2 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 4 Dec 2015 13:48:09 -0800 Subject: [PATCH 5/5] filters: refactor #sample to leave off the arg --- lib/jekyll/filters.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 6efc2e94..2b1bf1af 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -283,11 +283,11 @@ module Jekyll def sample(input, num = 1) return input unless input.respond_to?(:sample) - sampling = input.sample(num) - if num == 1 - sampling.first + n = num.to_i rescue 1 + if n == 1 + input.sample else - sampling + input.sample(n) end end