From 965aef60e6915521afc6ecf9b7c65c5e56398754 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Wed, 15 May 2019 21:13:13 +0530 Subject: [PATCH] Initialize mutations for Drops only if necessary (#7657) Merge pull request 7657 --- lib/jekyll/drops/drop.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/jekyll/drops/drop.rb b/lib/jekyll/drops/drop.rb index 315817fe..90c6cfaf 100644 --- a/lib/jekyll/drops/drop.rb +++ b/lib/jekyll/drops/drop.rb @@ -30,7 +30,6 @@ module Jekyll # Returns nothing def initialize(obj) @obj = obj - @mutations = {} # only if mutable: true end # Access a method in the Drop or a field in the underlying hash data. @@ -42,8 +41,8 @@ module Jekyll # # Returns the value for the given key, or nil if none exists def [](key) - if self.class.mutable? && @mutations.key?(key) - @mutations[key] + if self.class.mutable? && mutations.key?(key) + mutations[key] elsif self.class.invokable? key public_send key else @@ -70,7 +69,7 @@ module Jekyll public_send("#{key}=", val) elsif respond_to?(key.to_s) if self.class.mutable? - @mutations[key] = val + mutations[key] = val else raise Errors::DropMutationException, "Key #{key} cannot be set in the drop." end @@ -100,7 +99,7 @@ module Jekyll # Returns true if the given key is present def key?(key) return false if key.nil? - return true if self.class.mutable? && @mutations.key?(key) + return true if self.class.mutable? && mutations.key?(key) respond_to?(key) || fallback_data.key?(key) end @@ -113,7 +112,7 @@ module Jekyll # Returns an Array of unique keys for content for the Drop. def keys (content_methods | - @mutations.keys | + mutations.keys | fallback_data.keys).flatten end @@ -204,6 +203,12 @@ module Jekyll return yield(key) unless block.nil? return default unless default.nil? end + + private + + def mutations + @mutations ||= {} + end end end end