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 = RDoc::TopLevel.classes_hash[ename] ||
RDoc::TopLevel.modules_hash[ename]
unless enclosing then
enclosing = RDoc::TopLevel.classes_hash[given_name] || RDoc::TopLevel.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
superclass = nil if full_name == 'BasicObject'
superclass = nil if full_name == 'Object' and defined?(::BasicObject)
superclass = '::BasicObject' if
defined?(::BasicObject) and full_name == 'Object'
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 = RDoc::TopLevel.modules_hash.delete superclass
upgrade_to_class mod, RDoc::NormalClass, mod.parent if mod
superclass = nil if superclass == full_name
end
klass = RDoc::TopLevel.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 = RDoc::TopLevel.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,
RDoc::TopLevel.classes_hash)
end
end
klass
end