Refactor post and draft object creation

De-duplicate object creation between posts and drafts. Inject the name
dependency through a parameter
This commit is contained in:
Matt Rogers & Persa Zula 2013-12-05 22:54:18 -06:00 committed by Parker Moore
parent e14fe116db
commit e69d77b4df
1 changed files with 13 additions and 17 deletions

View File

@ -169,19 +169,14 @@ module Jekyll
# #
# Returns nothing. # Returns nothing.
def read_posts(dir) def read_posts(dir)
entries = get_entries(dir, '_posts') posts = read_things(dir, '_posts', Post)
# first pass processes, but does not yet render post content
entries.each do |f|
if Post.valid?(f)
post = Post.new(self, self.source, dir, f)
posts.each do |post|
if post.published && (self.future || post.date <= self.time) if post.published && (self.future || post.date <= self.time)
aggregate_post_info(post) aggregate_post_info(post)
end end
end end
end end
end
# Read all the files in <source>/<dir>/_drafts and create a new Post # Read all the files in <source>/<dir>/_drafts and create a new Post
# object with each one. # object with each one.
@ -190,16 +185,17 @@ module Jekyll
# #
# Returns nothing. # Returns nothing.
def read_drafts(dir) def read_drafts(dir)
entries = get_entries(dir, '_drafts') drafts = read_things(dir, '_drafts', Draft)
# first pass processes, but does not yet render draft content
entries.each do |f|
if Draft.valid?(f)
draft = Draft.new(self, self.source, dir, f)
drafts.each do |draft|
aggregate_post_info(draft) aggregate_post_info(draft)
end end
end end
def read_things(dir, magic_dir, klass)
get_entries(dir, magic_dir).each_with_object([]) do |entry, things|
things << klass.new(self, self.source, dir, entry) if klass.valid?(entry)
end
end end
# Read and parse all yaml files under <source>/<dir> # Read and parse all yaml files under <source>/<dir>