| Module | MUES::TypeCheckFunctions |
| In: |
lib/mues/mixins.rb
(CVS)
|
Mixin that adds some type-checking functions to the current scope
Check each object of anArray for implementations of requiredMethods, calling the optional errBlock if specified, or raising a TypeError if one of the methods is unimplemented.
# File lib/mues/mixins.rb, line 319 def checkEachResponse( anArray, *requiredMethods, &errBlock ) # :yeilds: method, object raise ScriptError, "First argument to checkEachResponse must be an array" unless anArray.is_a?( Array ) anArray.each do |anObject| if block_given? then checkResponse anObject, *requiredMethods, &errBlock else checkResponse( anObject, *requiredMethods ) {|method, object| raise TypeError, "Argument '#{anObject.inspect}' does "\ "not answer the '#{method}()' method", caller(1).find_all {|frame| frame.include?(__FILE__)} } end end return true end
Check each object in the specified objectArray with a call to checkType with the specified validTypes array.
# File lib/mues/mixins.rb, line 265 def checkEachType( objectArray, *validTypes, &errBlock ) # :yields: object, *validTypes raise ScriptError, "First argument to checkEachType must be an array" unless objectArray.is_a?( Array ) objectArray.each do |anObject| if block_given? then checkType anObject, validTypes, &errBlock else checkType( anObject, *validTypes ) {|obj, vTypes| typeList = vTypes.collect {|type| type.name}.join(" or ") raise TypeError, "Argument must be of type #{typeList}, not a #{obj.class.name}", caller(1).find_all {|frame| !frame.include?(__FILE__)} } end end return true end
Check anObject for implementations of requiredMethods. If one of the methods is unimplemented, and an optional block is given it is called with the method that failed the responds_to? test and the object being checked. If no handler block is given, a TypeError is raised.
# File lib/mues/mixins.rb, line 291 def checkResponse( anObject, *requiredMethods ) # yields method, anObject # Red: Throw away any nil types, and warn # Debug level might be inappropriate? os = requiredMethods.size requiredMethods.compact! debugMsg(1, "nil given in *requiredMethods") unless os == requiredMethods.size if requiredMethods.size > 0 then requiredMethods.each do |method| next if anObject.respond_to?( method ) if block_given? then yield( method, anObject ) else raise TypeError, "Argument '#{anObject.inspect}' does "\ "not answer the '#{method}()' method", caller(1).find_all {|frame| frame.include?(__FILE__)} end end end return true end
Check anObject to make sure it‘s one of the specified validTypes. If the object is not one of the specified value types, and an optional block is given it is called with the object being tested and the array of valid types. If no handler block is given, a TypeError is raised.
# File lib/mues/mixins.rb, line 227 def checkType( anObject, *validTypes ) # :yields: object, *validTypes validTypes.flatten! validTypes.compact! unless validTypes.empty? ### Compare the object against the array of valid types, and either ### yield to the error block if given or generate our own exception ### if not. unless validTypes.find {|type| anObject.kind_of?( type ) } then typeList = validTypes.collect {|type| type.name}.join(" or ") if block_given? then yield( anObject, [ *validTypes ].flatten ) else raise TypeError, "Argument must be of type #{typeList}, not a #{anObject.class.name}", caller(1).find_all {|frame| !frame.include?(__FILE__)} end end else if anObject.nil? then if block_given? then yield( anObject, *validTypes ) else raise ArgumentError, "Argument missing.", caller(1).find_all {|frame| !frame.include?(__FILE__)} end end end return true end