lib/symbolmatrix/symbolmatrix.rb in symbolmatrix-1.1.1 vs lib/symbolmatrix/symbolmatrix.rb in symbolmatrix-1.1.2
- old
+ new
@@ -67,38 +67,55 @@
self[sym]
end
end
# Merges this SymbolMatrix with another SymbolMatrix recursively
- def recursive_merge hash
+ def recursive_merge hash, override = false
result = SymbolMatrix.new
self.keys.each do |key|
result[key] = self[key]
end
hash.keys.each do |key|
if result.has_key? key
- result[key] = result[key].recursive_merge hash[key]
+ if result[key].respond_to? :recursive_merge
+ result[key] = result[key].recursive_merge hash[key], override
+ else
+ if override
+ result[key] = hash[key]
+ else
+ raise MergeError, "The value of the :#{key} key is already defined. Run recursive merge with flag true if you want it to override"
+ end
+ end
else
result[key] = hash[key]
end
end
return result
end
# Merges recursively the passed SymbolMatrix into self
- def recursive_merge! symbolmatrix
+ def recursive_merge! symbolmatrix, override = false
symbolmatrix.each do |key, value|
if self.has_key? key
- self[key].recursive_merge! value
+ if self[key].respond_to? :recursive_merge!
+ self[key].recursive_merge! value, override
+ else
+ if override
+ self[key] = value
+ else
+ raise MergeError, "The value of the :#{key} key is already defined. Run recursive merge with flag true if you want it to override"
+ end
+ end
else
self[key] = value
end
end
end
class KeyNotDefinedException < RuntimeError; end
class InvalidKeyException < RuntimeError; end
+ class MergeError < RuntimeError; end
end
def SymbolMatrix *args
SymbolMatrix.new *args
end
\ No newline at end of file