Merge pull request #4289 from jekyll/fix-utils-deep-merge-drops

Merge pull request 4289
This commit is contained in:
Parker Moore 2015-12-27 08:27:25 -05:00
commit 7c4876d2eb
4 changed files with 52 additions and 9 deletions

View File

@ -71,6 +71,18 @@ module Jekyll
end end
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. # Generates a list of keys with user content as their values.
# This gathers up the Drop methods and keys of the mutations and # This gathers up the Drop methods and keys of the mutations and
# underlying data hashes and performs a set union to ensure a list # 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. # Returns an Array of unique keys for content for the Drop.
def keys def keys
((self.class.instance_methods(false) - NON_CONTENT_METHODS).map(&:to_s) | (content_methods |
@mutations.keys | @mutations.keys |
fallback_data.keys).flatten fallback_data.keys).flatten
end end
@ -102,6 +114,15 @@ module Jekyll
JSON.pretty_generate to_h JSON.pretty_generate to_h
end 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 end
end end

View File

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

View File

@ -31,7 +31,8 @@ module Jekyll
# Thanks to whoever made it. # Thanks to whoever made it.
def deep_merge_hashes!(target, overwrite) def deep_merge_hashes!(target, overwrite)
overwrite.each_key do |key| 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]) target[key] = Utils.deep_merge_hashes(target[key], overwrite[key])
next next
end end
@ -39,7 +40,7 @@ module Jekyll
target[key] = overwrite[key] target[key] = overwrite[key]
end 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 target.default_proc = overwrite.default_proc
end end

View File

@ -1,6 +1,31 @@
require 'helper' require 'helper'
class TestUtils < JekyllUnitTest class TestUtils < JekyllUnitTest
context "The \`Utils.deep_merge_hashes\` method" do
setup do
clear_dest
@site = fixture_site
@site.process
end
should "merge a drop into a hash" do
data = {"page" => {}}
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
context "hash" do context "hash" do
context "pluralized_array" do context "pluralized_array" do