Skip to content

Commit 3d5fc2a

Browse files
committed
Extract upgrade_to_class
1 parent 9a18307 commit 3d5fc2a

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

lib/rdoc/context.rb

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,12 @@ def add_class class_type, given_name, superclass = '::Object'
325325
name = full_name.split(/:+/).last
326326
else
327327
full_name = child_name given_name
328+
328329
if full_name =~ /^(.+)::(\w+)$/ then
329330
name = $2
330331
ename = $1
331-
enclosing = RDoc::TopLevel.classes_hash[ename] || RDoc::TopLevel.modules_hash[ename]
332+
enclosing = RDoc::TopLevel.classes_hash[ename] ||
333+
RDoc::TopLevel.modules_hash[ename]
332334
# HACK: crashes in actionpack/lib/action_view/helpers/form_helper.rb (metaprogramming)
333335
unless enclosing then
334336
# try the given name at top level (will work for the above example)
@@ -338,7 +340,8 @@ def add_class class_type, given_name, superclass = '::Object'
338340
names = ename.split('::')
339341
enclosing = self
340342
names.each do |n|
341-
enclosing = enclosing.classes_hash[n] || enclosing.modules_hash[n] ||
343+
enclosing = enclosing.classes_hash[n] ||
344+
enclosing.modules_hash[n] ||
342345
enclosing.add_module(RDoc::NormalModule, n)
343346
end
344347
end
@@ -363,18 +366,10 @@ def add_class class_type, given_name, superclass = '::Object'
363366
end
364367
end
365368

366-
# did we believed it was a module?
367-
mod = RDoc::TopLevel.modules_hash.delete(superclass)
369+
# did we believe it was a module?
370+
mod = RDoc::TopLevel.modules_hash.delete superclass
368371

369-
if mod then
370-
super_context = mod.parent
371-
super_context.modules_hash.delete mod.name
372-
# TODO check: could it be RDoc::AnyClass?
373-
sklass = RDoc::ClassModule.from_module(RDoc::NormalClass, mod)
374-
# if it was there, then we keep it even if mod.parent.done_documenting
375-
RDoc::TopLevel.classes_hash[sklass.full_name] = sklass
376-
super_context.classes_hash[sklass.name] = sklass
377-
end
372+
upgrade_to_class mod, RDoc::NormalClass, mod.parent if mod
378373

379374
# e.g., Object < Object
380375
superclass = nil if superclass == full_name
@@ -396,17 +391,15 @@ def add_class class_type, given_name, superclass = '::Object'
396391
end
397392
else
398393
# this is a new class
399-
mod = RDoc::TopLevel.modules_hash.delete(full_name)
394+
mod = RDoc::TopLevel.modules_hash.delete full_name
395+
400396
if mod then
401-
# it was registered as a module, so transform it to a class
402-
enclosing.modules_hash.delete name
403-
klass = RDoc::ClassModule.from_module(class_type, mod)
397+
klass = upgrade_to_class mod, RDoc::NormalClass, enclosing
398+
404399
klass.superclass = superclass unless superclass.nil?
405-
# if it was there, then we keep it even if done_documenting
406-
RDoc::TopLevel.classes_hash[full_name] = klass
407-
enclosing.classes_hash[name] = klass
408400
else
409-
klass = class_type.new(name, superclass)
401+
klass = class_type.new name, superclass
402+
410403
enclosing.add_class_or_module(klass, enclosing.classes_hash,
411404
RDoc::TopLevel.classes_hash)
412405
end
@@ -1027,5 +1020,20 @@ def top_level
10271020
@top_level
10281021
end
10291022

1023+
##
1024+
# Upgrades NormalModule +mod+ in +enclosing+ to a +class_type+
1025+
1026+
def upgrade_to_class mod, class_type, enclosing
1027+
enclosing.modules_hash.delete mod.name
1028+
1029+
klass = RDoc::ClassModule.from_module class_type, mod
1030+
1031+
# if it was there, then we keep it even if done_documenting
1032+
RDoc::TopLevel.classes_hash[full_name] = klass
1033+
enclosing.classes_hash[name] = klass
1034+
1035+
klass
1036+
end
1037+
10301038
end
10311039

0 commit comments

Comments
 (0)