move all HashWithIndifferentAccess recursive enhancement over to the Util class

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
This commit is contained in:
Steven Sloan 2015-01-15 03:30:26 -05:00
parent a771b15700
commit 14e1cb1cd6
2 changed files with 6 additions and 20 deletions

View file

@ -80,8 +80,8 @@ module Middleman
# @private # @private
# @param [Hash] data Normal hash # @param [Hash] data Normal hash
# @return [Middleman::Util::HashWithIndifferentAccess] # @return [Middleman::Util::HashWithIndifferentAccess]
FrozenDataStructure = Frozen[Or[HashWithIndifferentAccess, Array]] FrozenDataStructure = Frozen[Or[HashWithIndifferentAccess, Array, String, TrueClass, FalseClass, Fixnum]]
Contract Maybe[Or[Array, Hash, HashWithIndifferentAccess]] => Maybe[FrozenDataStructure] Contract Maybe[Or[String, Array, Hash, HashWithIndifferentAccess]] => Maybe[FrozenDataStructure]
def recursively_enhance(data) def recursively_enhance(data)
if data.is_a? HashWithIndifferentAccess if data.is_a? HashWithIndifferentAccess
data data
@ -89,8 +89,10 @@ module Middleman
HashWithIndifferentAccess.new(data) HashWithIndifferentAccess.new(data)
elsif data.is_a? Array elsif data.is_a? Array
data.map(&method(:recursively_enhance)).freeze data.map(&method(:recursively_enhance)).freeze
elsif data.frozen? || data.nil? || [::TrueClass, ::FalseClass, ::Fixnum].include?(data.class)
data
else else
nil data.dup.freeze
end end
end end

View file

@ -19,7 +19,7 @@ module Middleman
super() super()
hash.each do |key, val| hash.each do |key, val|
self[key] = recursively_enhance(val) self[key] = Util.recursively_enhance(val)
end end
freeze freeze
@ -82,22 +82,6 @@ module Middleman
end end
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 end
end end