Initialize mutations for Drops only if necessary (#7657)

Merge pull request 7657
This commit is contained in:
Ashwin Maroli 2019-05-15 21:13:13 +05:30 committed by jekyllbot
parent 6a604daa5a
commit 965aef60e6
1 changed files with 11 additions and 6 deletions

View File

@ -30,7 +30,6 @@ module Jekyll
# Returns nothing # Returns nothing
def initialize(obj) def initialize(obj)
@obj = obj @obj = obj
@mutations = {} # only if mutable: true
end end
# Access a method in the Drop or a field in the underlying hash data. # 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 # Returns the value for the given key, or nil if none exists
def [](key) def [](key)
if self.class.mutable? && @mutations.key?(key) if self.class.mutable? && mutations.key?(key)
@mutations[key] mutations[key]
elsif self.class.invokable? key elsif self.class.invokable? key
public_send key public_send key
else else
@ -70,7 +69,7 @@ module Jekyll
public_send("#{key}=", val) public_send("#{key}=", val)
elsif respond_to?(key.to_s) elsif respond_to?(key.to_s)
if self.class.mutable? if self.class.mutable?
@mutations[key] = val mutations[key] = val
else else
raise Errors::DropMutationException, "Key #{key} cannot be set in the drop." raise Errors::DropMutationException, "Key #{key} cannot be set in the drop."
end end
@ -100,7 +99,7 @@ module Jekyll
# Returns true if the given key is present # Returns true if the given key is present
def key?(key) def key?(key)
return false if key.nil? 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) respond_to?(key) || fallback_data.key?(key)
end end
@ -113,7 +112,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
(content_methods | (content_methods |
@mutations.keys | mutations.keys |
fallback_data.keys).flatten fallback_data.keys).flatten
end end
@ -204,6 +203,12 @@ module Jekyll
return yield(key) unless block.nil? return yield(key) unless block.nil?
return default unless default.nil? return default unless default.nil?
end end
private
def mutations
@mutations ||= {}
end
end end
end end
end end