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:
parent
7fbe61fc77
commit
2e7c471c70
|
@ -23,12 +23,21 @@ module Jekyll
|
||||||
@docs ||= []
|
@docs ||= []
|
||||||
end
|
end
|
||||||
|
|
||||||
[:sort, :sort!, :each, :[], :reject, :first, :last, :size, :length].each do |method|
|
# Override of normal respond_to? to match method_missing's logic for
|
||||||
class_eval %Q"
|
# looking in @data.
|
||||||
def #{method}(*args, &blk)
|
def respond_to?(method, include_private = false)
|
||||||
docs.#{method}(*args, &blk)
|
docs.respond_to?(method.to_sym, include_private) || super
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
# Fetch the static files in this collection.
|
# Fetch the static files in this collection.
|
||||||
|
|
|
@ -431,5 +431,22 @@ module Jekyll
|
||||||
def related_posts
|
def related_posts
|
||||||
Jekyll::RelatedPosts.new(self).build
|
Jekyll::RelatedPosts.new(self).build
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,7 +22,7 @@ module Jekyll
|
||||||
|
|
||||||
# Sorts posts, pages, and static files.
|
# Sorts posts, pages, and static files.
|
||||||
def sort_files!
|
def sort_files!
|
||||||
site.collections.values.each(&:sort!)
|
site.collections.values.each{|c| c.docs.sort!}
|
||||||
site.pages.sort_by!(&:name)
|
site.pages.sort_by!(&:name)
|
||||||
site.static_files.sort_by!(&:relative_path)
|
site.static_files.sort_by!(&:relative_path)
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,7 +14,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def build
|
def build
|
||||||
return [] unless site.posts.size > 1
|
return [] unless site.posts.docs.size > 1
|
||||||
|
|
||||||
if site.lsi
|
if site.lsi
|
||||||
build_index
|
build_index
|
||||||
|
@ -30,7 +30,7 @@ module Jekyll
|
||||||
lsi = ClassifierReborn::LSI.new(:auto_rebuild => false)
|
lsi = ClassifierReborn::LSI.new(:auto_rebuild => false)
|
||||||
display("Populating LSI...")
|
display("Populating LSI...")
|
||||||
|
|
||||||
site.posts.each do |x|
|
site.posts.docs.each do |x|
|
||||||
lsi.add_item(x)
|
lsi.add_item(x)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -222,7 +222,7 @@ module Jekyll
|
||||||
# Build a hash map based on the specified post attribute ( post attr =>
|
# Build a hash map based on the specified post attribute ( post attr =>
|
||||||
# array of posts ) then sort each array in reverse order.
|
# array of posts ) then sort each array in reverse order.
|
||||||
hash = Hash.new { |h, key| h[key] = [] }
|
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.values.each { |posts| posts.sort!.reverse! }
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
@ -265,7 +265,7 @@ module Jekyll
|
||||||
"site" => Utils.deep_merge_hashes(config,
|
"site" => Utils.deep_merge_hashes(config,
|
||||||
Utils.deep_merge_hashes(Hash[collections.map{|label, coll| [label, coll.docs]}], {
|
Utils.deep_merge_hashes(Hash[collections.map{|label, coll| [label, coll.docs]}], {
|
||||||
"time" => time,
|
"time" => time,
|
||||||
"posts" => posts.sort { |a, b| b <=> a },
|
"posts" => posts.docs.sort { |a, b| b <=> a },
|
||||||
"pages" => pages,
|
"pages" => pages,
|
||||||
"static_files" => static_files,
|
"static_files" => static_files,
|
||||||
"html_pages" => pages.select { |page| page.html? || page.url.end_with?("/") },
|
"html_pages" => pages.select { |page| page.html? || page.url.end_with?("/") },
|
||||||
|
@ -333,7 +333,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def each_site_file
|
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|
|
send(type).each do |item|
|
||||||
yield item
|
yield item
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue