Class: SemiStatic::Posts
- Inherits:
-
Object
- Object
- SemiStatic::Posts
- Defined in:
- lib/semi-static/posts.rb
Overview
Collection of Posts for the current site.
Constant Summary collapse
- NAME_RE =
The format of the Date portion of a post’s filename.
/^([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)/
Instance Attribute Summary collapse
-
#indices ⇒ Object
readonly
Indices that correspond to the Posts.
-
#names ⇒ Object
readonly
Posts indexed by name.
-
#posts ⇒ Object
readonly
The raw list of posts.
-
#site ⇒ Object
readonly
The Site object we belong to.
Instance Method Summary collapse
-
#<<(source_path) ⇒ Object
Parse the given file as a Post and add it to the list.
-
#[](name) ⇒ Object
Get the post with the given name.
-
#chop!(count) ⇒ Object
Truncate the list of Posts to the given number (to speed up testing).
-
#each(&block) ⇒ Object
Iterate over all posts.
-
#each_index(&block) ⇒ Object
Iterate over all indices.
-
#first(n = nil) ⇒ Object
Fetch the least recent posts.
-
#from(year, month = nil, day = nil) ⇒ Object
Fetch the Posts for the given Date range.
-
#initialize(site) ⇒ Posts
constructor
Initializes a new Posts.
-
#last(n = nil) ⇒ Object
Fetch the most recent posts.
-
#length ⇒ Object
Number of posts.
Constructor Details
#initialize(site) ⇒ Posts
Initializes a new Posts
site: The Site we belong to.
25 26 27 28 29 30 |
# File 'lib/semi-static/posts.rb', line 25 def initialize(site) @site = site @posts = [] @names = {} @indices = Set.new end |
Instance Attribute Details
#indices ⇒ Object (readonly)
Indices that correspond to the Posts.
19 20 21 |
# File 'lib/semi-static/posts.rb', line 19 def indices @indices end |
#names ⇒ Object (readonly)
Posts indexed by name.
15 16 17 |
# File 'lib/semi-static/posts.rb', line 15 def names @names end |
#posts ⇒ Object (readonly)
The raw list of posts.
11 12 13 |
# File 'lib/semi-static/posts.rb', line 11 def posts @posts end |
#site ⇒ Object (readonly)
The Site object we belong to.
7 8 9 |
# File 'lib/semi-static/posts.rb', line 7 def site @site end |
Instance Method Details
#<<(source_path) ⇒ Object
Parse the given file as a Post and add it to the list.
source_path: Path to the source file
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/semi-static/posts.rb', line 40 def <<(source_path) unless File.file?(source_path) $stderr.puts "[#{source_path}]" return end if source_path =~ NAME_RE post = Post.new site, source_path self.posts.push post names[post.name] = post indices << File.join(post.year, post.month, post.day) indices << File.join(post.year, post.month) indices << post.year else $stderr.puts "{#{source_path}}" end end |
#[](name) ⇒ Object
Get the post with the given name.
142 143 144 |
# File 'lib/semi-static/posts.rb', line 142 def [](name) return self.names[name] end |
#chop!(count) ⇒ Object
Truncate the list of Posts to the given number (to speed up testing).
count: The number of posts to keep.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/semi-static/posts.rb', line 61 def chop!(count) raise ArgumentError unless count > 0 posts = self.posts.first(count) @posts = [] @names = {} @indices = Set.new for post in posts self.posts << post names[post.name] = post indices << File.join(post.year, post.month, post.day) indices << File.join(post.year, post.month) indices << post.year end end |
#each(&block) ⇒ Object
Iterate over all posts.
148 149 150 |
# File 'lib/semi-static/posts.rb', line 148 def each(&block) # :yields: Post self.posts.each(&block) end |
#each_index(&block) ⇒ Object
Iterate over all indices.
154 155 156 |
# File 'lib/semi-static/posts.rb', line 154 def each_index(&block) # :yields: String indices.each(&block) end |
#first(n = nil) ⇒ Object
Fetch the least recent posts.
n: Number of Posts to return.
86 87 88 89 90 91 92 |
# File 'lib/semi-static/posts.rb', line 86 def first(n=nil) if n.nil? self.posts.first else self.posts.first(n) end end |
#from(year, month = nil, day = nil) ⇒ Object
Fetch the Posts for the given Date range.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/semi-static/posts.rb', line 108 def from(year, month=nil, day=nil) if year.is_a?(String) && month.nil? && day.nil? date = year.split('/', 3) year, month, day = date.collect { |c| c.to_i } end if month.nil? from = Time.local year to = Time.local(year + 1) - 1 elsif day.nil? from = Time.local year, month if month == 12 to = Time.local(year + 1, 1) - 1 else to = Time.local(year, month + 1) - 1 end else from = Time.local year, month, day to = Time.local year, month, day, 23, 59, 59 end range = from..to result = self.posts.select { |p| range.include?(p.created) } class << result alias_method :last_without_reverse, :last def last_with_reverse(n=nil) last_without_reverse(n).reverse end alias_method :last, :last_with_reverse end return result end |
#last(n = nil) ⇒ Object
Fetch the most recent posts.
n: Number of Posts to return.
98 99 100 101 102 103 104 |
# File 'lib/semi-static/posts.rb', line 98 def last(n=nil) if n.nil? self.posts.last else self.posts.last(n).reverse end end |
#length ⇒ Object
Number of posts.
78 79 80 |
# File 'lib/semi-static/posts.rb', line 78 def length self.posts.length end |