add_class(class_type, given_name, superclass = '::Object')
public
Adds a class named given_name with superclass.
Both given_name and superclass may contain ‘::’, and
are interpreted relative to the self context. This allows handling
correctly examples like these:
class RDoc::Gauntlet < Gauntlet
module Mod
class Object
class SubObject < Object
Given class Container::Item RDoc assumes
Container is a module unless it later sees class
Container. add_class automatically
upgrades given_name to a class in this case.
Show source
def add_class class_type, given_name, superclass = '::Object'
if given_name =~ /^:+(\w+)$/ then
full_name = $1
enclosing = top_level
name = full_name.split(/:+/).last
else
full_name = child_name given_name
if full_name =~ /^(.+)::(\w+)$/ then
name = $2
ename = $1
enclosing = @store.classes_hash[ename] || @store.modules_hash[ename]
unless enclosing then
enclosing = @store.classes_hash[given_name] ||
@store.modules_hash[given_name]
return enclosing if enclosing
names = ename.split('::')
enclosing = self
names.each do |n|
enclosing = enclosing.classes_hash[n] ||
enclosing.modules_hash[n] ||
enclosing.add_module(RDoc::NormalModule, n)
end
end
else
name = full_name
enclosing = self
end
end
if full_name == 'BasicObject' then
superclass = nil
elsif full_name == 'Object' then
superclass = '::BasicObject'
end
if superclass then
if superclass =~ /^:+/ then
superclass = $'
else
if superclass =~ /^(\w+):+(.+)$/ then
suffix = $2
mod = find_module_named($1)
superclass = mod.full_name + '::' + suffix if mod
else
mod = find_module_named(superclass)
superclass = mod.full_name if mod
end
end
mod = @store.modules_hash.delete superclass
upgrade_to_class mod, RDoc::NormalClass, mod.parent if mod
superclass = nil if superclass == full_name
end
klass = @store.classes_hash[full_name]
if klass then
enclosing.classes_hash[name] = klass
if superclass then
existing = klass.superclass
existing = existing.full_name unless existing.is_a?(String) if existing
if existing.nil? ||
(existing == 'Object' && superclass != 'Object') then
klass.superclass = superclass
end
end
else
mod = @store.modules_hash.delete full_name
if mod then
klass = upgrade_to_class mod, RDoc::NormalClass, enclosing
klass.superclass = superclass unless superclass.nil?
else
klass = class_type.new name, superclass
enclosing.add_class_or_module(klass, enclosing.classes_hash,
@store.classes_hash)
end
end
klass.parent = self
klass
end