Add Document#method_missing and Collection#method_missing

Proxies calls to either #data or #docs, respectively. Deprecation warning is printed.
This commit is contained in:
Parker Moore 2015-10-26 13:37:04 -07:00
parent 7fbe61fc77
commit 2e7c471c70
5 changed files with 38 additions and 12 deletions

View File

@ -23,12 +23,21 @@ module Jekyll
@docs ||= []
end
[:sort, :sort!, :each, :[], :reject, :first, :last, :size, :length].each do |method|
class_eval %Q"
def #{method}(*args, &blk)
docs.#{method}(*args, &blk)
end
"
# Override of normal respond_to? to match method_missing's logic for
# looking in @data.
def respond_to?(method, include_private = false)
docs.respond_to?(method.to_sym, include_private) || super
end
# Override of method_missing to check in @data for the key.
def method_missing(method, *args, &blck)
if docs.respond_to?(method.to_sym)
Jekyll.logger.warn "Deprecation:", "Collection##{method} should be called on the #docs array directly."
Jekyll.logger.warn "", "Called by #{caller.first}."
docs.public_send(method.to_sym, *args, &blck)
else
super
end
end
# Fetch the static files in this collection.

View File

@ -431,5 +431,22 @@ module Jekyll
def related_posts
Jekyll::RelatedPosts.new(self).build
end
# Override of normal respond_to? to match method_missing's logic for
# looking in @data.
def respond_to?(method, include_private = false)
data.key?(method.to_s) || super
end
# Override of method_missing to check in @data for the key.
def method_missing(method, *args, &blck)
if data.key?(method.to_s)
Jekyll.logger.warn "Deprecation:", "Document##{method} is now a key in the #data hash."
Jekyll.logger.warn "", "Called by #{caller.first}."
data[method.to_s]
else
super
end
end
end
end

View File

@ -22,7 +22,7 @@ module Jekyll
# Sorts posts, pages, and static files.
def sort_files!
site.collections.values.each(&:sort!)
site.collections.values.each{|c| c.docs.sort!}
site.pages.sort_by!(&:name)
site.static_files.sort_by!(&:relative_path)
end

View File

@ -14,7 +14,7 @@ module Jekyll
end
def build
return [] unless site.posts.size > 1
return [] unless site.posts.docs.size > 1
if site.lsi
build_index
@ -30,7 +30,7 @@ module Jekyll
lsi = ClassifierReborn::LSI.new(:auto_rebuild => false)
display("Populating LSI...")
site.posts.each do |x|
site.posts.docs.each do |x|
lsi.add_item(x)
end

View File

@ -222,7 +222,7 @@ module Jekyll
# Build a hash map based on the specified post attribute ( post attr =>
# array of posts ) then sort each array in reverse order.
hash = Hash.new { |h, key| h[key] = [] }
posts.each { |p| p.data[post_attr].each { |t| hash[t] << p } }
posts.docs.each { |p| p.data[post_attr].each { |t| hash[t] << p } }
hash.values.each { |posts| posts.sort!.reverse! }
hash
end
@ -265,7 +265,7 @@ module Jekyll
"site" => Utils.deep_merge_hashes(config,
Utils.deep_merge_hashes(Hash[collections.map{|label, coll| [label, coll.docs]}], {
"time" => time,
"posts" => posts.sort { |a, b| b <=> a },
"posts" => posts.docs.sort { |a, b| b <=> a },
"pages" => pages,
"static_files" => static_files,
"html_pages" => pages.select { |page| page.html? || page.url.end_with?("/") },
@ -333,7 +333,7 @@ module Jekyll
end
def each_site_file
%w(posts pages static_files docs_to_write).each do |type|
%w(pages static_files docs_to_write).each do |type|
send(type).each do |item|
yield item
end