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 f25a5f69..bf7e8957 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -1,6 +1,31 @@ require 'helper' 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 "pluralized_array" do