Method: BrocadeREST::RestController#deepCompare

Defined in:
lib/brocade/restcontroller.rb

#deepCompare(name, hash1, hash2, failFast, errors = 0) ⇒ Object

Recurse over the hash and compare all elements. Simply using hash == hash can fail when the keys are in a different order.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/brocade/restcontroller.rb', line 201

def deepCompare(name, hash1, hash2, failFast, errors=0)
	if hash1.is_a?(Hash)
		if hash2.nil?
			logger(1,"DeepCompare: #{name}, No Match '#{hash1}' vs 'nil'")
			@errors.push("DeepCompare: #{name}, No Match '#{hash1}' vs 'nil'")
			failFast ? (return false) : (return errors+1)
		end
		hash1.each do |key,value|
			if ( hash2.include?(key) )
				result = deepCompare("#{name}:#{key}", value, hash2[key], failFast, errors)
				( failFast and (!result) ) ? (return false) : errors = result
			else
				logger(1,"DeepCompare: #{name}, Missing Key '#{key}'")
				@errors.push("DeepCompare: #{name}, Missing Key '#{key}'")
				failFast ? (return false) : (return errors+1)
			end
		end
		failFast ? (return true) : (return errors)
	elsif hash1.is_a?(Array)
		# Check array lengths match
		if hash1.length != hash2.length
			logger(1, "DeepCompare: #{name}, Size differs: #{hash1.length} vs #{hash2.length}")
			@errors.push("DeepCompare: #{name}, Size differs: #{hash1.length} vs #{hash2.length}")
			failFast ? (return false) : (return errors+1)
		end
		# sort arrays before comparing
		sort1 = hash1.sort_by { |h| h.to_s }
		sort2 = hash2.sort_by { |h| h.to_s }
		sort1.each do |item1|
			item2 = sort2.shift
			result = deepCompare("#{name}:#{item1}", item1, item2, failFast, errors)
			( failFast and (!result) ) ? ( return false ) : errors = result
		end	
		failFast ? ( return true ) : ( return errors )
	else
		if ( hash1 != hash2 )
			logger(1,"DeepCompare: #{name}, No Match '#{hash1}' vs '#{hash2}'")
			@errors.push("DeepCompare: #{name}, No Match: #{hash1} vs #{hash2}")
			failFast ? (return false) : (return errors+1)
		end
	end
	failFast ? (return true) : (return errors)
end