utils/drops: update Drop to support Utils.deep_merge_hashes

Fixes #4287
This commit is contained in:
Parker Moore 2015-12-27 08:06:37 -05:00
parent 69a6323599
commit 5bf596b239
4 changed files with 42 additions and 14 deletions

View File

@ -71,6 +71,18 @@ module Jekyll
end
end
# Generates a list of strings which correspond to content getter
# methods.
#
# Returns an Array of strings which represent method-specific keys.
def content_methods
@content_methods ||= (
self.class.instance_methods(false) - NON_CONTENT_METHODS
).map(&:to_s).reject do |method|
method.end_with?("=")
end
end
# Generates a list of keys with user content as their values.
# This gathers up the Drop methods and keys of the mutations and
# underlying data hashes and performs a set union to ensure a list
@ -78,7 +90,7 @@ module Jekyll
#
# Returns an Array of unique keys for content for the Drop.
def keys
((self.class.instance_methods(false) - NON_CONTENT_METHODS).map(&:to_s) |
(content_methods |
@mutations.keys |
fallback_data.keys).flatten
end
@ -102,6 +114,15 @@ module Jekyll
JSON.pretty_generate to_h
end
# Collects all the keys and passes each to the block in turn.
#
# block - a block which accepts one argument, the key
#
# Returns nothing.
def each_key(&block)
keys.each(&block)
end
end
end
end

View File

@ -3,21 +3,17 @@
module Jekyll
module Drops
class UnifiedPayloadDrop < Drop
mutable false
mutable true
attr_accessor :page, :layout, :content, :paginator
attr_accessor :highlighter_prefix, :highlighter_suffix
def initialize(site)
@site = site
end
def jekyll
JekyllDrop.global
end
def site
@site_drop ||= SiteDrop.new(@site)
@site_drop ||= SiteDrop.new(@obj)
end
private

View File

@ -31,7 +31,8 @@ module Jekyll
# Thanks to whoever made it.
def deep_merge_hashes!(target, overwrite)
overwrite.each_key do |key|
if overwrite[key].is_a? Hash and target[key].is_a? Hash
if (overwrite[key].is_a?(Hash) or overwrite[key].is_a?(Drops::Drop)) and
(target[key].is_a?(Hash) or target[key].is_a?(Drops::Drop))
target[key] = Utils.deep_merge_hashes(target[key], overwrite[key])
next
end
@ -39,7 +40,7 @@ module Jekyll
target[key] = overwrite[key]
end
if target.default_proc.nil?
if target.respond_to?(:default_proc) && overwrite.respond_to?(:default_proc) && target.default_proc.nil?
target.default_proc = overwrite.default_proc
end

View File

@ -4,15 +4,25 @@ class TestUtils < JekyllUnitTest
context "The \`Utils.deep_merge_hashes\` method" do
setup do
clear_dest
config = Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir})
@site = fixture_site(config)
@site = fixture_site
@site.process
end
should "merge a page into the site" do
should "merge a drop into a hash" do
data = {"page" => {}}
assert Utils.deep_merge_hashes(data, @site.site_payload)
merged = Utils.deep_merge_hashes(data, @site.site_payload)
assert merged.is_a? Hash
assert merged["site"].is_a? Drops::SiteDrop
assert_equal data["page"], {}
end
should "merge a hash into a drop" do
data = {"page" => {}}
assert_nil @site.site_payload["page"]
merged = Utils.deep_merge_hashes(@site.site_payload, data)
assert merged.is_a? Drops::UnifiedPayloadDrop
assert merged["site"].is_a? Drops::SiteDrop
assert_equal data["page"], {}
end
end