Method: ActiveFacts::API::ObjectType#supertypes

Defined in:
lib/activefacts/api/object_type.rb

#supertypes(*object_types) ⇒ Object

Access supertypes or add new supertypes; multiple inheritance. With parameters (Class objects), it adds new supertypes to this class. Instances of this class will then have role methods for any new superclasses (transitively). Superclasses must be Ruby classes which are existing ObjectTypes. Without parameters, it returns the array of ObjectType supertypes (one by Ruby inheritance, any others as defined using this method)



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/activefacts/api/object_type.rb', line 129

def supertypes(*object_types)
  @supertypes ||= []
  all_supertypes = supertypes_transitive
  object_types.each do |object_type|
    next if all_supertypes.include? object_type
    supertype =
      case object_type
      when Class
        object_type
      when Symbol
        # No late binding here:
        (object_type = vocabulary.const_get(object_type.to_s.camelcase))
      else
        raise InvalidSupertypeException.new("Illegal supertype #{object_type.inspect} for #{self.class.basename}")
      end
    unless supertype.respond_to?(:vocabulary) and supertype.vocabulary == self.vocabulary
      raise InvalidSupertypeException.new("#{supertype.name} must be an object type in #{vocabulary.name}")
    end

    if is_entity_type != supertype.is_entity_type
      raise InvalidSupertypeException.new("#{self} < #{supertype}: A value type may not be a supertype of an entity type, and vice versa")
    end

    TypeInheritanceFactType.new(supertype, self)
    @supertypes << supertype

    # Realise the roles (create accessors) of this supertype.
    realise_supertypes(object_type, all_supertypes)
  end
  [(superclass.respond_to?(:vocabulary) ? superclass : nil), *@supertypes].compact
end