Sha256: 85ec6ec9ba7a1605b33a758c527dace3e86823f6afded11c2690fc64a76545ca

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

# encoding: utf-8
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)

require "multitype-introspection"

module Types
    class Type
    
        ##
        # Returns classes which are part of this type.
        #
        
        def type_classes
            raise Exception::new("Class is abstract.")
        end
        
        ##
        # Returns types which are part of this type.
        # 
        
        def type_types
            [ ]
        end
        
        ##
        # Matches object is of this type.
        #
        
        def match_type?(object)
            result = object.kind_of_any? self.type_classes
            if not result
                result = object.type_of_any? self.type_types
            end
            
            return result
        end
        
    end
    
    class Boolean < Type

        ##
        # Returns classes which are part of this type.
        # In case of boolean <tt>TrueClass</tt> and <tt>FalseClass</tt>.
        #
        
        def type_classes
            [TrueClass, FalseClass]
        end
        
    end
end

class Object

    ##
    # Indicates object is type of some class.
    # If class isn't Type, matches against kind_of?.
    #
    
    def type_of?(cls)
        cls_new = cls::new
        if cls_new.kind_of? Types::Type
            cls_new.match_type? self
        else
            self.kind_of? cls
        end
    end


    ##
    # Indicates object is type of some class in the list.
    # If class isn't Type, matches against kind_of?.
    #
        
    def type_of_any?(classes)
        if not classes.kind_of? Array
            raise Exception::new("Array expected.")
        end
        
        classes.each do |cls|
            if self.type_of? cls
                return true
            end
        end
        
        return false
    end
end

class Boolean < Types::Boolean
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
types-0.1.0 lib/types.rb