From 14e1cb1cd62f9953953ac6d13e017cd277b31783 Mon Sep 17 00:00:00 2001 From: Steven Sloan Date: Thu, 15 Jan 2015 03:30:26 -0500 Subject: [PATCH] move all HashWithIndifferentAccess recursive enhancement over to the Util class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: moving this two a class method on HashWithIndifferentAccess would really make the most sense, but there are two blockers: - historically the method has operated out of Util. While totally ok, simply dedeferring to HashWithIndifferentAccess feels a bit awkward - the contracts gem doesn’t support class method at this time. the assurance of frozen output is important considering the uses of the recursively_enhance method --- middleman-core/lib/middleman-core/util.rb | 8 +++++--- .../util/hash_with_indifferent_access.rb | 18 +----------------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 188b32d5..5cbf8828 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -80,8 +80,8 @@ module Middleman # @private # @param [Hash] data Normal hash # @return [Middleman::Util::HashWithIndifferentAccess] - FrozenDataStructure = Frozen[Or[HashWithIndifferentAccess, Array]] - Contract Maybe[Or[Array, Hash, HashWithIndifferentAccess]] => Maybe[FrozenDataStructure] + FrozenDataStructure = Frozen[Or[HashWithIndifferentAccess, Array, String, TrueClass, FalseClass, Fixnum]] + Contract Maybe[Or[String, Array, Hash, HashWithIndifferentAccess]] => Maybe[FrozenDataStructure] def recursively_enhance(data) if data.is_a? HashWithIndifferentAccess data @@ -89,8 +89,10 @@ module Middleman HashWithIndifferentAccess.new(data) elsif data.is_a? Array data.map(&method(:recursively_enhance)).freeze + elsif data.frozen? || data.nil? || [::TrueClass, ::FalseClass, ::Fixnum].include?(data.class) + data else - nil + data.dup.freeze end end diff --git a/middleman-core/lib/middleman-core/util/hash_with_indifferent_access.rb b/middleman-core/lib/middleman-core/util/hash_with_indifferent_access.rb index 3c934d9c..a401e0f1 100644 --- a/middleman-core/lib/middleman-core/util/hash_with_indifferent_access.rb +++ b/middleman-core/lib/middleman-core/util/hash_with_indifferent_access.rb @@ -19,7 +19,7 @@ module Middleman super() hash.each do |key, val| - self[key] = recursively_enhance(val) + self[key] = Util.recursively_enhance(val) end freeze @@ -82,22 +82,6 @@ module Middleman end end - private - - Contract Any => Frozen[Any] - def recursively_enhance(data) - if data.is_a? HashWithIndifferentAccess - data - elsif data.is_a? Hash - self.class.new(data) - elsif data.is_a? Array - data.map(&method(:recursively_enhance)).freeze - elsif data.frozen? || data.nil? || [::TrueClass, ::FalseClass, ::Fixnum].include?(data.class) - data - else - data.dup.freeze - end - end end end end