Class: Plate::PostCollection
- Inherits:
-
Object
- Object
- Plate::PostCollection
- Includes:
- Enumerable
- Defined in:
- lib/plate/post_collection.rb
Overview
Post collection is an enumerable wrapper for the posts in a site.
Instance Attribute Summary collapse
-
#archives ⇒ Object
Returns the value of attribute archives.
-
#categories ⇒ Object
Returns the value of attribute categories.
-
#tags ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
-
#<<(post) ⇒ Object
Add a post to the collection.
-
#add(post) ⇒ Object
Add a post to the collection, then add its meta data to the summary.
-
#category_counts ⇒ Object
A hash of all categories in this collection with the number of posts using each.
-
#category_list ⇒ Object
A sorted array of all categories in this collection.
-
#each ⇒ Object
Loop through each Post.
-
#initialize ⇒ PostCollection
constructor
A new instance of PostCollection.
-
#last(*args) ⇒ Object
Returns the last post in the collection.
-
#method_missing(method, *args, &block) ⇒ Object
Any methods called on the collection can be passed through to the Array.
-
#size ⇒ Object
Size of the posts collection.
-
#tag_counts ⇒ Object
A hash of all tags in this collection with the number of posts using that tag.
-
#tag_list ⇒ Object
A sorted array of all tag names in this collection.
- #to_a ⇒ Object
- #years ⇒ Object
Constructor Details
#initialize ⇒ PostCollection
Returns a new instance of PostCollection.
8 9 10 11 12 13 |
# File 'lib/plate/post_collection.rb', line 8 def initialize @posts = [] @categories = {} @tags = {} @archives = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Any methods called on the collection can be passed through to the Array
64 65 66 |
# File 'lib/plate/post_collection.rb', line 64 def method_missing(method, *args, &block) @posts.send(method, *args, &block) end |
Instance Attribute Details
#archives ⇒ Object
Returns the value of attribute archives.
6 7 8 |
# File 'lib/plate/post_collection.rb', line 6 def archives @archives end |
#categories ⇒ Object
Returns the value of attribute categories.
6 7 8 |
# File 'lib/plate/post_collection.rb', line 6 def categories @categories end |
#tags ⇒ Object
Returns the value of attribute tags.
6 7 8 |
# File 'lib/plate/post_collection.rb', line 6 def @tags end |
Instance Method Details
#<<(post) ⇒ Object
Add a post to the collection
16 17 18 |
# File 'lib/plate/post_collection.rb', line 16 def <<(post) add(post) end |
#add(post) ⇒ Object
Add a post to the collection, then add its meta data to the summary.
21 22 23 24 25 |
# File 'lib/plate/post_collection.rb', line 21 def add(post) return nil unless Post === post @posts << post (post) end |
#category_counts ⇒ Object
A hash of all categories in this collection with the number of posts using each.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/plate/post_collection.rb', line 28 def category_counts return @category_counts if @category_counts result = {} categories.keys.each do |key| result[key] = categories[key].size end @category_counts = result end |
#category_list ⇒ Object
A sorted array of all categories in this collection.
41 42 43 |
# File 'lib/plate/post_collection.rb', line 41 def category_list @category_list ||= categories.keys.sort end |
#each ⇒ Object
Loop through each Post
46 47 48 |
# File 'lib/plate/post_collection.rb', line 46 def each @posts.each { |post| yield post } end |
#last(*args) ⇒ Object
Returns the last post in the collection.
Or, pass in a number to return in descending order that number of posts
53 54 55 56 57 58 59 60 61 |
# File 'lib/plate/post_collection.rb', line 53 def last(*args) result = @posts.last(*args) if Array === result result.reverse! end result end |
#size ⇒ Object
Size of the posts collection
69 70 71 |
# File 'lib/plate/post_collection.rb', line 69 def size @posts.size end |
#tag_counts ⇒ Object
A hash of all tags in this collection with the number of posts using that tag.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/plate/post_collection.rb', line 74 def tag_counts return @tag_counts if @tag_counts result = {} .keys.each do |key| result[key] = [key].size end @tag_counts = result end |
#tag_list ⇒ Object
A sorted array of all tag names in this collection.
87 88 89 |
# File 'lib/plate/post_collection.rb', line 87 def tag_list @tag_list ||= .keys.sort end |
#to_a ⇒ Object
91 92 93 |
# File 'lib/plate/post_collection.rb', line 91 def to_a @posts end |
#years ⇒ Object
95 96 97 |
# File 'lib/plate/post_collection.rb', line 95 def years @years ||= archives.keys.sort end |