Created
March 24, 2014 08:08
-
-
Save bitben/9736126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CategoriesTags | |
extend Nanoc::Memoization | |
def all_categories | |
categories = [] | |
posts.each do |post| | |
next if post[:categories].nil? | |
categories << post[:categories] | |
end | |
categories.uniq | |
end | |
memoize :all_categories | |
def has_category?(category, post) | |
if post[:categories].nil? | |
false | |
else | |
post[:categories].include?(category) | |
end | |
end | |
def news_with_category(category, news=sorted_posts) | |
news.select { |post| has_category?(category, post) } | |
end | |
memoize :news_with_category | |
def news_by_category(news=posts) | |
categories = [] | |
all_categories.each do |category| | |
categories << [category, news_with_category(category)] | |
end | |
categories | |
end | |
memoize :news_by_category | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment