method
add_class
v2_5_5 -
Show latest stable
- Class:
RDoc::Context
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 # implies < ::Object class SubObject < Object # this is _not_ ::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.
# File lib/rdoc/context.rb, line 290
def add_class class_type, given_name, superclass = '::Object'
# superclass +nil+ is passed by the C parser in the following cases:
# - registering Object in 1.8 (correct)
# - registering BasicObject in 1.9 (correct)
# - registering RubyVM in 1.9 in iseq.c (incorrect: < Object in vm.c)
#
# If we later find a superclass for a registered class with a nil
# superclass, we must honor it.
# find the name & enclosing context
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]
# HACK: crashes in actionpack/lib/action_view/helpers/form_helper.rb (metaprogramming)
unless enclosing then
# try the given name at top level (will work for the above example)
enclosing = @store.classes_hash[given_name] ||
@store.modules_hash[given_name]
return enclosing if enclosing
# not found: create the parent(s)
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
# fix up superclass
if full_name == 'BasicObject' then
superclass = nil
elsif full_name == 'Object' then
superclass = '::BasicObject'
end
# find the superclass full name
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
# did we believe it was a module?
mod = @store.modules_hash.delete superclass
upgrade_to_class mod, RDoc::NormalClass, mod.parent if mod
# e.g., Object < Object
superclass = nil if superclass == full_name
end
klass = @store.classes_hash[full_name]
if klass then
# if TopLevel, it may not be registered in the classes:
enclosing.classes_hash[name] = klass
# update the superclass if needed
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
# this is a new class
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 Related methods
- Instance methods
- <=>
- add
- add_alias
- add_attribute
- add_class
- add_class_or_module
- add_constant
- add_extend
- add_include
- add_method
- add_module
- add_module_alias
- add_require
- add_section
- add_to
- any_content
- child_name
- class_attributes
- class_method_list
- classes
- classes_and_modules
- classes_hash
- current_section
- defined_in?
- display
- each_ancestor
- each_attribute
- each_classmodule
- each_constant
- each_extend
- each_include
- each_method
- each_section
- find_attribute
- find_attribute_named
- find_class_method_named
- find_constant_named
- find_enclosing_module_named
- find_external_alias
- find_external_alias_named
- find_file_named
- find_instance_method_named
- find_local_symbol
- find_method
- find_method_named
- find_module_named
- find_symbol
- find_symbol_module
- full_name
- fully_documented?
- http_url
- initialize_methods_etc
- instance_attributes
- instance_method_list
- methods_by_type
- methods_matching
- modules
- modules_hash
- name_for_path
- ongoing_visibility=
- record_location
- remove_from_documentation?
- remove_invisible
- remove_invisible_in
- resolve_aliases
- section_contents
- sections
- sections_hash
- set_constant_visibility_for
- set_current_section
- set_visibility_for
- sort_sections
- to_s
- top_level
- upgrade_to_class
- Class methods
- new