Class: Plate::PostCollection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializePostCollection

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

#archivesObject

Returns the value of attribute archives.



6
7
8
# File 'lib/plate/post_collection.rb', line 6

def archives
  @archives
end

#categoriesObject

Returns the value of attribute categories.



6
7
8
# File 'lib/plate/post_collection.rb', line 6

def categories
  @categories
end

#tagsObject

Returns the value of attribute tags.



6
7
8
# File 'lib/plate/post_collection.rb', line 6

def tags
  @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_countsObject

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_listObject

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

#eachObject

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

#sizeObject

Size of the posts collection



69
70
71
# File 'lib/plate/post_collection.rb', line 69

def size
  @posts.size
end

#tag_countsObject

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 = {}
  
  tags.keys.each do |key|
    result[key] = tags[key].size
  end
  
  @tag_counts = result
end

#tag_listObject

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 ||= tags.keys.sort
end

#to_aObject



91
92
93
# File 'lib/plate/post_collection.rb', line 91

def to_a
  @posts
end

#yearsObject



95
96
97
# File 'lib/plate/post_collection.rb', line 95

def years
  @years ||= archives.keys.sort
end