Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Implement group_by Liquid filter & tests. #1788

Merged
merged 2 commits into from

2 participants

@parkr
Owner

Supercedes #1738.

lib/jekyll/filters.rb
((4 lines not shown))
+
+ # Group an array of items by a property
+ #
+ # input - the inputted Enumerable
+ # property - the property
+ #
+ # Returns an array of Hashes, each looking something like this:
+ # {"name" => "larry"
+ # "items" => [...] } # all the items where `property` == "larry"
+ def group_by(input, property)
+ if groupable?(input)
+ input.group_by do |item|
+ if item.respond_to?(:data)
+ item.data[property.to_s].to_s
+ else
+ item[property.to_s].to_s
@mattr- Owner
mattr- added a note

Perhaps this

if item.respond_to?(:data)
 item.data[property.to_s].to_s
else
 item[property.to_s].to_s
end

gets it's own property_data(property) method?

@parkr Owner
parkr added a note

I was just thinking that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mattr-
Owner

Just the one question but :+1: otherwise.

@parkr parkr merged commit a70726e into master
@parkr parkr referenced this pull request from a commit
@parkr parkr Update history to reflect merge of #1788 6e74ec2
@parkr parkr deleted the group-by-filter-2 branch
@mattr-
Owner

:heart:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 8, 2013
  1. @parkr
Commits on Dec 9, 2013
  1. @parkr
This page is out of date. Refresh to see the latest.
Showing with 60 additions and 5 deletions.
  1. +34 −1 lib/jekyll/filters.rb
  2. +26 −4 test/test_filters.rb
View
35 lib/jekyll/filters.rb
@@ -100,7 +100,7 @@ def xml_escape(input)
def cgi_escape(input)
CGI::escape(input)
end
-
+
# URI escape a string.
#
# input - The String to escape.
@@ -158,6 +158,27 @@ def jsonify(input)
input.to_json
end
+
+ # Group an array of items by a property
+ #
+ # input - the inputted Enumerable
+ # property - the property
+ #
+ # Returns an array of Hashes, each looking something like this:
+ # {"name" => "larry"
+ # "items" => [...] } # all the items where `property` == "larry"
+ def group_by(input, property)
+ if groupable?(input)
+ input.group_by do |item|
+ item_property(item, property).to_s
+ end.inject([]) do |memo, i|
+ memo << {"name" => i.first, "items" => i.last}
+ end
+ else
+ input
+ end
+ end
+
private
def time(input)
case input
@@ -170,5 +191,17 @@ def time(input)
exit(1)
end
end
+
+ def groupable?(element)
+ element.respond_to?(:group_by)
+ end
+
+ def item_property(item, property)
+ if item.respond_to?(:data)
+ item.data[property.to_s]
+ else
+ item[property.to_s]
+ end
+ end
end
end
View
30 test/test_filters.rb
@@ -3,16 +3,17 @@
class TestFilters < Test::Unit::TestCase
class JekyllFilter
include Jekyll::Filters
+ attr_accessor :site, :context
- def initialize
- site = Jekyll::Site.new(Jekyll.configuration({}))
- @context = Liquid::Context.new({}, {}, { :site => site })
+ def initialize(opts = {})
+ @site = Jekyll::Site.new(Jekyll.configuration(opts))
+ @context = Liquid::Context.new({}, {}, { :site => @site })
end
end
context "filters" do
setup do
- @filter = JekyllFilter.new
+ @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"
end
@@ -109,5 +110,26 @@ def initialize
assert_equal "[{\"name\":\"Jack\"},{\"name\":\"Smith\"}]", @filter.jsonify([{:name => 'Jack'}, {:name => 'Smith'}])
end
end
+
+ context "group_by filter" do
+ should "successfully group array of Jekyll::Page's" do
+ @filter.site.process
+ grouping = @filter.group_by(@filter.site.pages, "layout")
+ grouping.each do |g|
+ assert ["default", "nil", ""].include?(g["name"]), "#{g['name']} isn't a valid grouping."
+ case g["name"]
+ when "default"
+ assert g["items"].is_a?(Array), "The list of grouped items for 'default' is not an Array."
+ assert_equal 3, g["items"].size
+ when "nil"
+ assert g["items"].is_a?(Array), "The list of grouped items for 'nil' is not an Array."
+ assert_equal 2, g["items"].size
+ when ""
+ assert g["items"].is_a?(Array), "The list of grouped items for '' is not an Array."
+ assert_equal 5, g["items"].size
+ end
+ end
+ end
+ end
end
end
Something went wrong with that request. Please try again.