From 69a6323599ea1c902097d958892611fc9b672a51 Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Sat, 26 Dec 2015 12:37:08 -0800 Subject: [PATCH 1/2] Utils.deep_merge_hashes failing test --- test/test_utils.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/test_utils.rb b/test/test_utils.rb index f25a5f69..42d4f3ed 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -1,6 +1,21 @@ require 'helper' 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.process + end + + should "merge a page into the site" do + data = {"page" => {}} + assert Utils.deep_merge_hashes(data, @site.site_payload) + end + end + context "hash" do context "pluralized_array" do From 5bf596b2390ec716cad3da81eece9f2c08dc6f96 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 27 Dec 2015 08:06:37 -0500 Subject: [PATCH 2/2] utils/drops: update Drop to support Utils.deep_merge_hashes Fixes #4287 --- lib/jekyll/drops/drop.rb | 23 ++++++++++++++++++++++- lib/jekyll/drops/unified_payload_drop.rb | 8 ++------ lib/jekyll/utils.rb | 5 +++-- test/test_utils.rb | 20 +++++++++++++++----- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/lib/jekyll/drops/drop.rb b/lib/jekyll/drops/drop.rb index b14581a4..98eb864a 100644 --- a/lib/jekyll/drops/drop.rb +++ b/lib/jekyll/drops/drop.rb @@ -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 diff --git a/lib/jekyll/drops/unified_payload_drop.rb b/lib/jekyll/drops/unified_payload_drop.rb index 3c593ba6..26b9104b 100644 --- a/lib/jekyll/drops/unified_payload_drop.rb +++ b/lib/jekyll/drops/unified_payload_drop.rb @@ -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 diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index a2c6139d..c0c56579 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -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 diff --git a/test/test_utils.rb b/test/test_utils.rb index 42d4f3ed..bf7e8957 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -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