Merge pull request #1426 from stevenosloan/standardize_recursively_enhance_for_hash_with_indifferent_access

Standardize recursively enhance for hash with indifferent access
This commit is contained in:
Thomas Reynolds 2015-01-15 09:43:13 +00:00
commit 0409c5bcbe
3 changed files with 7 additions and 21 deletions

View file

@ -187,7 +187,7 @@ module Middleman
end
def key?(key)
@local_data.key?(key.to_s) || data_for_path(key)
( @local_data.keys + @local_sources.keys + @callback_sources.keys ).include?(key.to_s)
end
alias_method :has_key?, :key?

View file

@ -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

View file

@ -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