# # File 'recursivemerge.rb' created on 17 mar 2008 at 09:37:25. # # See 'dokkit.rb' or +LICENSE+ for licence information. # # (C) 2008 Andrea Fazzi (and contributors). # class Hash def recursive_store(key, value, other_value = nil) if(value.class == Hash && other_value.class == Hash) value.recursive_merge other_value else store(key, other_value) end end # Hash#recursive_merge merges two arbitrarily deep hashes into a single hash. # The hash is followed recursively, so that deeply nested hashes that are at # the same level will be merged when the parent hashes are merged. def recursive_merge!(other_hash, &blk) if block_given? merge!(other_hash, &blk) else merge!(other_hash) do |key, value, other_value| recursive_store(key, value, other_value) end end end # Non-destructive version of Hash#recursive_merge method. def recursive_merge(other_hash, &blk) clone.recursive_merge!(other_hash, &blk) end end module ConfigHash def recursive_store(key, value, other_value) unless (key.to_sym == :layout || key.to_sym == :config) if(value.class == Hash && other_value.class == Hash) value.extend ConfigHash other_value.extend ConfigHash value.recursive_merge other_value elsif(value.class == Array && other_value.class == Array) value.concat(other_value) elsif(value.class == String && other_value.class == Array) value.to_a.concat(other_value) elsif(value.class == Array && other_value.class == String) value << other_value else store(key, other_value) end else if(value.class == Hash && other_value.class == Hash) value.recursive_merge other_value else store(key, other_value) end end end end