Class: RDoc::ClassModule

Inherits:
Context show all
Defined in:
lib/rdoc/code_object/class_module.rb,
lib/rdoc/generator/markup.rb

Overview

ClassModule is the base class for objects representing either a class or a module.

Direct Known Subclasses

NormalClass, NormalModule, SingleClass

Constant Summary collapse

MARSHAL_VERSION =
1

RDoc 3.7

  • Added visibility, singleton and file to attributes

  • Added file to constants

  • Added file to includes

  • Added file to methods

2

RDoc 3.13

  • Added extends

3

RDoc 4.0

  • Added sections

  • Added in_files

  • Added parent name

  • Complete Constant dump

3

Constants inherited from Context

RDoc::Context::TOMDOC_TITLES, RDoc::Context::TOMDOC_TITLES_SORT, RDoc::Context::TYPES

Constants included from Text

Text::MARKUP_FORMAT, Text::SPACE_SEPARATED_LETTER_CLASS

Instance Attribute Summary collapse

Attributes inherited from Context

#aliases, #attributes, #block_params, #constants, #constants_hash, #current_line_visibility, #current_section, #extends, #external_aliases, #in_files, #includes, #method_list, #methods_hash, #name, #params, #requires, #temporary_section, #unmatched_alias_lists, #visibility

Attributes inherited from CodeObject

#comment, #document_children, #document_self, #done_documenting, #file, #force_documentation, #line, #metadata, #mixin_from, #parent, #received_nodoc, #section, #store

Attributes included from Text

#language

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Context

#<=>, #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_module_by_normal_module, #add_require, #add_section, #add_to, #any_content, #child_name, #class_method_list, #classes, #classes_and_modules, #classes_hash, #display, #each_classmodule, #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_instance_method_named, #find_local_symbol, #find_method, #find_method_named, #find_module_named, #find_or_create_constant_owner_for_path, #find_or_create_namespace_path, #find_symbol, #find_symbol_module, #fully_documented?, #get_module_named, #http_url, #initialize_methods_etc, #instance_methods, #methods_by_type, #methods_matching, #modules, #modules_hash, #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, #top_level, #upgrade_to_class

Methods inherited from CodeObject

#display?, #file_name, #full_name=, #ignore, #ignored?, #initialize_visibility, #options, #parent_name, #record_location, #start_doc, #stop_doc, #suppress, #suppressed?

Methods included from Generator::Markup

#aref_to, #as_href, #canonical_url, #cvs_url, #formatter

Methods included from Text

decode_legacy_label, expand_tabs, #flush_left, #markup, #normalize_comment, #snippet, #strip_hashes, #strip_newlines, #strip_stars, to_anchor, #wrap

Constructor Details

#initialize(name, superclass = nil) ⇒ ClassModule

Creates a new ClassModule with name with optional superclass

This is a constructor for subclasses, and must never be called directly.



121
122
123
124
125
126
127
128
129
# File 'lib/rdoc/code_object/class_module.rb', line 121

def initialize(name, superclass = nil)
  @constant_aliases = []
  @is_alias_for     = nil
  @name             = name
  @superclass       = superclass
  @comment_location = {} # Hash of { location => [comments] }

  super()
end

Instance Attribute Details

#comment_locationObject

A hash of { location => [comments] } documenting this class/module. Use #add_comment to add comments.

Ruby hashes maintain insertion order, so comments render in the order they were first added. Each location maps to an array of comments, allowing a class reopened in the same file to accumulate multiple comments.

Before marshalling:

  • location is an RDoc::TopLevel

  • comments are Strings

After unmarshalling:

  • location is a filename String

  • comments are RDoc::Markup::Documents



48
49
50
# File 'lib/rdoc/code_object/class_module.rb', line 48

def comment_location
  @comment_location
end

#constant_aliasesObject

Constants that are aliases for this class or module



30
31
32
# File 'lib/rdoc/code_object/class_module.rb', line 30

def constant_aliases
  @constant_aliases
end

#is_alias_forObject

Class or module this constant is an alias for



53
54
55
# File 'lib/rdoc/code_object/class_module.rb', line 53

def is_alias_for
  @is_alias_for
end

Class Method Details

.from_module(class_type, mod) ⇒ Object

Return a RDoc::ClassModule of class class_type that is a copy of module module. Used to promote modules to classes. – TODO move to RDoc::NormalClass (I think)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rdoc/code_object/class_module.rb', line 61

def self.from_module(class_type, mod)
  klass = class_type.new mod.name

  mod.comment_location.each do |location, comments|
    comments.each { |comment| klass.add_comment comment, location }
  end

  klass.parent = mod.parent
  klass.section = mod.section

  klass.attributes.concat mod.attributes
  klass.method_list.concat mod.method_list
  klass.aliases.concat mod.aliases
  klass.external_aliases.concat mod.external_aliases
  klass.constants.concat mod.constants
  klass.includes.concat mod.includes
  klass.extends.concat mod.extends

  klass.methods_hash.update mod.methods_hash
  klass.constants_hash.update mod.constants_hash

  klass.current_section = mod.current_section
  klass.in_files.concat mod.in_files
  klass.sections.concat mod.sections
  klass.unmatched_alias_lists = mod.unmatched_alias_lists
  klass.current_section = mod.current_section
  klass.visibility = mod.visibility

  klass.classes_hash.update mod.classes_hash
  klass.modules_hash.update mod.modules_hash
  klass..update mod.

  klass.document_self = mod.received_nodoc ? nil : mod.document_self
  klass.document_children = mod.document_children
  klass.force_documentation = mod.force_documentation
  klass.done_documenting = mod.done_documenting

  # update the parent of all children

  (klass.attributes +
   klass.method_list +
   klass.aliases +
   klass.external_aliases +
   klass.constants +
   klass.includes +
   klass.extends +
   klass.classes +
   klass.modules).each do |obj|
    obj.parent = klass
    obj.full_name = nil
  end

  klass
end

Instance Method Details

#add_comment(comment, location) ⇒ Object

Adds comment to this ClassModule’s list of comments at location. This method is preferred over #comment= since it allows ri data to be updated across multiple runs.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rdoc/code_object/class_module.rb', line 136

def add_comment(comment, location)
  return unless document_self

  original = comment

  comment = case comment
            when RDoc::Comment then
              comment.normalize
            else
              normalize_comment comment
            end

  (@comment_location[location] ||= []) << comment

  self.comment = original
end

#add_things(my_things, other_things) ⇒ Object

:nodoc:



153
154
155
156
157
158
159
160
161
162
# File 'lib/rdoc/code_object/class_module.rb', line 153

def add_things(my_things, other_things) # :nodoc:
  other_things.each do |group, things|
    my_things[group].each { |thing| yield false, thing } if
      my_things.include? group

    things.each do |thing|
      yield true, thing
    end
  end
end

#ancestorsObject Also known as: direct_ancestors

Ancestors list for this ClassModule: the list of included modules (classes will add their superclass if any).

Returns the included classes or modules, not the includes themselves. The returned values are either String or RDoc::NormalModule instances (see RDoc::Include#module).

The values are returned in reverse order of their inclusion, which is the order suitable for searching methods/attributes in the ancestors. The superclass, if any, comes last.



176
177
178
# File 'lib/rdoc/code_object/class_module.rb', line 176

def ancestors
  includes.map { |i| i.module }.reverse
end

#arefObject

HTML fragment reference for this module or class using GitHub-style

anchor format (lowercase,

replaced with -).

Examples:

Foo      -> class-foo
Foo::Bar -> class-foo-bar


192
193
194
# File 'lib/rdoc/code_object/class_module.rb', line 192

def aref
  "#{aref_prefix}-#{full_name.downcase.gsub('::', '-')}"
end

#aref_prefixObject

:nodoc:

Raises:

  • (NotImplementedError)


180
181
182
# File 'lib/rdoc/code_object/class_module.rb', line 180

def aref_prefix # :nodoc:
  raise NotImplementedError, "missing aref_prefix for #{self.class}"
end

#clear_commentObject

Clears the comment. Used by the Ruby parser.



216
217
218
# File 'lib/rdoc/code_object/class_module.rb', line 216

def clear_comment
  @comment = ''
end

#comment=(comment) ⇒ Object

This method is deprecated, use #add_comment instead.

Appends comment to the current comment, but separated by a rule. Works more like +=.



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rdoc/code_object/class_module.rb', line 226

def comment=(comment) # :nodoc:
  comment = case comment
            when RDoc::Comment then
              comment.normalize
            else
              normalize_comment comment
            end

  comment = "#{@comment.to_s}\n---\n#{comment.to_s}" unless @comment.empty?

  super comment
end

#complete(min_visibility) ⇒ Object

Prepares this ClassModule for use by a generator.

See RDoc::Store#complete



244
245
246
247
248
249
250
251
# File 'lib/rdoc/code_object/class_module.rb', line 244

def complete(min_visibility)
  update_aliases
  remove_nodoc_children
  embed_mixins
  update_includes
  update_extends
  remove_invisible min_visibility
end

#descriptionObject

Handy wrapper for marking up this class or module’s comment



168
169
170
# File 'lib/rdoc/generator/markup.rb', line 168

def description
  markup @comment_location
end

#document_self_or_methodsObject

Does this ClassModule or any of its methods have document_self set?



256
257
258
# File 'lib/rdoc/code_object/class_module.rb', line 256

def document_self_or_methods
  document_self || method_list.any?{ |m| m.document_self }
end

#documented?Boolean

Does this class or module have a comment with content or is #received_nodoc true?

Returns:

  • (Boolean)


264
265
266
267
268
# File 'lib/rdoc/code_object/class_module.rb', line 264

def documented?
  return true if @received_nodoc
  return false if @comment_location.empty?
  @comment_location.each_value.any? { |comments| comments.any? { |c| not c.empty? } }
end

#each_ancestorObject

Iterates the ancestors of this class or module for which an RDoc::ClassModule exists.



274
275
276
277
278
279
280
281
282
# File 'lib/rdoc/code_object/class_module.rb', line 274

def each_ancestor # :yields: module
  return enum_for __method__ unless block_given?

  ancestors.each do |mod|
    next if String === mod
    next if self == mod
    yield mod
  end
end

#embed_mixinsObject



930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
# File 'lib/rdoc/code_object/class_module.rb', line 930

def embed_mixins
  return unless options.embed_mixins

  includes.each do |include|
    next if String === include.module
    include.module.method_list.each do |code_object|
      add_method(prepare_to_embed(code_object))
    end
    include.module.constants.each do |code_object|
      add_constant(prepare_to_embed(code_object))
    end
    include.module.attributes.each do |code_object|
      add_attribute(prepare_to_embed(code_object))
    end
  end

  extends.each do |ext|
    next if String === ext.module
    ext.module.method_list.each do |code_object|
      add_method(prepare_to_embed(code_object, true))
    end
    ext.module.attributes.each do |code_object|
      add_attribute(prepare_to_embed(code_object, true))
    end
  end
end

#find_ancestor_local_symbol(symbol) ⇒ Object

Looks for a symbol in the #ancestors. See Context#find_local_symbol.



287
288
289
290
291
292
293
294
# File 'lib/rdoc/code_object/class_module.rb', line 287

def find_ancestor_local_symbol(symbol)
  each_ancestor do |m|
    res = m.find_local_symbol(symbol)
    return res if res
  end

  nil
end

#find_class_named(name) ⇒ Object

Finds a class or module with name in this namespace or its descendants



299
300
301
302
303
304
305
306
307
# File 'lib/rdoc/code_object/class_module.rb', line 299

def find_class_named(name)
  return self if full_name == name
  return self if @name == name

  @classes.values.find do |klass|
    next if klass == self
    klass.find_class_named name
  end
end

#full_nameObject

Return the fully qualified name of this class or module



312
313
314
315
316
317
318
# File 'lib/rdoc/code_object/class_module.rb', line 312

def full_name
  @full_name ||= if RDoc::ClassModule === parent then
                   "#{parent.full_name}::#{@name}"
                 else
                   @name
                 end
end

#fully_qualified_nesting_namespacesObject

Return array of fully qualified nesting namespaces.

For example, if full_name is A::B::C, this method returns ["A", "A::B", "A::B::C"]



332
333
334
335
336
337
# File 'lib/rdoc/code_object/class_module.rb', line 332

def fully_qualified_nesting_namespaces
  return nesting_namespaces if nesting_namespaces.length < 2
  @fqns ||= nesting_namespaces.inject([]) do |list, n|
    list << (list.empty? ? n : "#{list.last}::#{n}")
  end
end

#legacy_arefObject

Legacy HTML fragment reference for backward compatibility. Returns the old RDoc-style anchor format.

Examples:

Foo      -> class-Foo
Foo::Bar -> class-Foo::Bar


204
205
206
# File 'lib/rdoc/code_object/class_module.rb', line 204

def legacy_aref
  "#{aref_prefix}-#{full_name}"
end

#marshal_dumpObject

TODO: filter included items by #display?



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/rdoc/code_object/class_module.rb', line 342

def marshal_dump # :nodoc:
  attrs = attributes.sort.map do |attr|
    next unless attr.display?
    [ attr.name, attr.rw,
      attr.visibility, attr.singleton, attr.file_name,
    ]
  end.compact

  method_types = methods_by_type.map do |type, visibilities|
    visibilities = visibilities.map do |visibility, methods|
      method_names = methods.map do |method|
        next unless method.display?
        [method.name, method.file_name]
      end.compact

      [visibility, method_names.uniq]
    end

    [type, visibilities]
  end

  [ MARSHAL_VERSION,
    @name,
    full_name,
    @superclass,
    parse(@comment_location),
    attrs,
    constants.select { |constant| constant.display? },
    includes.map do |incl|
      next unless incl.display?
      [incl.name, parse(incl.comment), incl.file_name]
    end.compact,
    method_types,
    extends.map do |ext|
      next unless ext.display?
      [ext.name, parse(ext.comment), ext.file_name]
    end.compact,
    @sections.values,
    @in_files.map do |tl|
      tl.relative_name
    end,
    parent.full_name,
    parent.class,
  ]
end

#marshal_load(array) ⇒ Object

:nodoc:



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/rdoc/code_object/class_module.rb', line 388

def marshal_load(array) # :nodoc:
  initialize_visibility
  initialize_methods_etc
  @current_section   = nil
  @document_self     = true
  @done_documenting  = false
  @parent            = nil
  @temporary_section = nil
  @visibility        = nil
  @classes           = {}
  @modules           = {}

  @name       = array[1]
  @full_name  = array[2]
  @superclass = array[3]
  document    = array[4]

  @comment    = RDoc::Comment.from_document document

  @comment_location = if document.parts.first.is_a?(RDoc::Markup::Document)
                        document.parts.group_by(&:file)
                      else
                        { document.file => [document] }
                      end

  array[5].each do |name, rw, visibility, singleton, file|
    singleton  ||= false
    visibility ||= :public

    attr = RDoc::Attr.new name, rw, nil, singleton: singleton

    add_attribute attr
    attr.visibility = visibility
    attr.record_location RDoc::TopLevel.new file
  end

  array[6].each do |constant, document, file|
    case constant
    when RDoc::Constant then
      add_constant constant
    else
      constant = add_constant RDoc::Constant.new(constant, nil, RDoc::Comment.from_document(document))
      constant.record_location RDoc::TopLevel.new file
    end
  end

  array[7].each do |name, document, file|
    incl = add_include RDoc::Include.new(name, RDoc::Comment.from_document(document))
    incl.record_location RDoc::TopLevel.new file
  end

  array[8].each do |type, visibilities|
    visibilities.each do |visibility, methods|
      @visibility = visibility

      methods.each do |name, file|
        method = RDoc::AnyMethod.new name, singleton: type == 'class'
        method.record_location RDoc::TopLevel.new file
        add_method method
      end
    end
  end

  array[9].each do |name, document, file|
    ext = add_extend RDoc::Extend.new(name, RDoc::Comment.from_document(document))
    ext.record_location RDoc::TopLevel.new file
  end if array[9] # Support Marshal version 1

  sections = (array[10] || []).map do |section|
    [section.title, section]
  end

  @sections = Hash[*sections.flatten]
  @current_section = add_section nil

  @in_files = []

  (array[11] || []).each do |filename|
    record_location RDoc::TopLevel.new filename
  end

  @parent_name  = array[12]
  @parent_class = array[13]
end

#merge(class_module) ⇒ Object

Merges class_module into this ClassModule.

The data in class_module is preferred over the receiver.



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/rdoc/code_object/class_module.rb', line 478

def merge(class_module)
  @parent      = class_module.parent
  @parent_name = class_module.parent_name

  other_document = parse class_module.comment_location

  if other_document then
    document = parse @comment_location

    document = document.merge other_document

    @comment = RDoc::Comment.from_document(document)

    @comment_location = if document.parts.first.is_a?(RDoc::Markup::Document)
                          document.parts.group_by(&:file)
                        else
                          { document.file => [document] }
                        end
  end

  cm = class_module
  other_files = cm.in_files

  merge_collections attributes, cm.attributes, other_files do |add, attr|
    if add then
      add_attribute attr
    else
      @attributes.delete attr
      @methods_hash.delete attr.pretty_name
    end
  end

  merge_collections constants, cm.constants, other_files do |add, const|
    if add then
      add_constant const
    else
      @constants.delete const
      @constants_hash.delete const.name
    end
  end

  merge_collections includes, cm.includes, other_files do |add, incl|
    if add then
      add_include incl
    else
      @includes.delete incl
    end
  end

  @includes.uniq! # clean up

  merge_collections extends, cm.extends, other_files do |add, ext|
    if add then
      add_extend ext
    else
      @extends.delete ext
    end
  end

  @extends.uniq! # clean up

  merge_collections method_list, cm.method_list, other_files do |add, meth|
    if add then
      add_method meth
    else
      @method_list.delete meth
      @methods_hash.delete meth.pretty_name
    end
  end

  merge_sections cm

  self
end

#merge_collections(mine, other, other_files, &block) ⇒ Object

Merges collection mine with other preferring other. other_files is used to help determine which items should be deleted.

Yields whether the item should be added or removed (true or false) and the item to be added or removed.

merge_collections things, other.things, other.in_files do |add, thing|
  if add then
    # add the thing
  else
    # remove the thing
  end
end


568
569
570
571
572
573
574
# File 'lib/rdoc/code_object/class_module.rb', line 568

def merge_collections(mine, other, other_files, &block) # :nodoc:
  my_things    = mine. group_by { |thing| thing.file }
  other_things = other.group_by { |thing| thing.file }

  remove_things my_things, other_files,  &block
  add_things    my_things, other_things, &block
end

#merge_sections(cm) ⇒ Object

Merges the comments in this ClassModule with the comments in the other ClassModule cm.



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/rdoc/code_object/class_module.rb', line 580

def merge_sections(cm) # :nodoc:
  my_sections    =    sections.group_by { |section| section.title }
  other_sections = cm.sections.group_by { |section| section.title }

  other_files = cm.in_files

  remove_things my_sections, other_files do |_, section|
    @sections.delete section.title
  end

  other_sections.each do |group, sections|
    if my_sections.include? group
      my_sections[group].each do |my_section|
        other_section = cm.sections_hash[group]

        my_comments    = my_section.comments
        other_comments = other_section.comments

        other_files = other_section.in_files

        merge_collections my_comments, other_comments, other_files do |add, comment|
          if add then
            my_section.add_comment comment
          else
            my_section.remove_comment comment
          end
        end
      end
    else
      sections.each do |section|
        add_section group, section.comments
      end
    end
  end
end

#module?Boolean

Does this object represent a module?

Returns:

  • (Boolean)


619
620
621
# File 'lib/rdoc/code_object/class_module.rb', line 619

def module?
  false
end

#name=(new_name) ⇒ Object

Allows overriding the initial name.

Used for modules and classes that are constant aliases.



628
629
630
# File 'lib/rdoc/code_object/class_module.rb', line 628

def name=(new_name)
  @name = new_name
end

#name_for_pathObject

Name to use to generate the url: modules and classes that are aliases for another module or class return the name of the latter.



675
676
677
# File 'lib/rdoc/code_object/class_module.rb', line 675

def name_for_path
  is_alias_for ? is_alias_for.name_for_path : full_name
end

#nesting_namespacesObject

Return array of full_name splitted by ::.



323
324
325
# File 'lib/rdoc/code_object/class_module.rb', line 323

def nesting_namespaces
  @namespaces ||= full_name.split("::").reject(&:empty?)
end

#non_aliasesObject

Returns the classes and modules that are not constants aliasing another class or module. For use by formatters only (caches its result).



684
685
686
# File 'lib/rdoc/code_object/class_module.rb', line 684

def non_aliases
  @non_aliases ||= classes_and_modules.reject { |cm| cm.is_alias_for }
end

#parse(comment_location) ⇒ Object

Parses comment_location into an RDoc::Markup::Document composed of multiple RDoc::Markup::Documents with their file set.



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/rdoc/code_object/class_module.rb', line 636

def parse(comment_location)
  case comment_location
  when String then
    super
  when Hash then
    docs = comment_location.flat_map do |location, comments|
      comments.map do |comment|
        doc = super comment
        doc.file = location
        doc
      end
    end

    RDoc::Markup::Document.new(*docs)
  when RDoc::Comment then
    doc = super comment_location.text, comment_location.format
    doc.file = comment_location.location
    doc
  when RDoc::Markup::Document then
    return comment_location
  else
    raise ArgumentError, "unknown comment class #{comment_location.class}"
  end
end

#pathObject

Path to this class or module for use with HTML generator output.



664
665
666
667
668
# File 'lib/rdoc/code_object/class_module.rb', line 664

def path
  prefix = options.class_module_path_prefix
  return http_url unless prefix
  File.join(prefix, http_url)
end

#rebuild_comment_from_locationObject

Rebuilds @comment from the current @comment_location entries, skipping any empty placeholders.



754
755
756
757
758
759
760
# File 'lib/rdoc/code_object/class_module.rb', line 754

def rebuild_comment_from_location
  texts = @comment_location.each_value.flat_map { |comments|
    comments.filter_map { |c| c.to_s unless c.empty? }
  }
  merged = texts.join("\n---\n")
  @comment = merged.empty? ? '' : RDoc::Comment.new(merged)
end

#remove_nodoc_childrenObject

Updates the child modules or classes of class/module parent by deleting the ones that have been removed from the documentation.

parent_hash is either parent.modules_hash or parent.classes_hash and all_hash is ::all_modules_hash or ::all_classes_hash.



696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/rdoc/code_object/class_module.rb', line 696

def remove_nodoc_children
  prefix = self.full_name + '::'

  modules_hash.each_key do |name|
    full_name = prefix + name
    modules_hash.delete name unless @store.modules_hash[full_name]
  end

  classes_hash.each_key do |name|
    full_name = prefix + name
    classes_hash.delete name unless @store.classes_hash[full_name]
  end
end

#remove_things(my_things, other_files) ⇒ Object

:nodoc:



710
711
712
713
714
715
716
717
718
719
720
# File 'lib/rdoc/code_object/class_module.rb', line 710

def remove_things(my_things, other_files) # :nodoc:
  my_things.delete_if do |file, things|
    next false unless other_files.include? file

    things.each do |thing|
      yield false, thing
    end

    true
  end
end

#search_recordObject

Search record used by RDoc::Generator::JsonIndex

TODO: Remove this method after dropping the darkfish theme and JsonIndex generator. Use #search_snippet instead for getting documentation snippets.



728
729
730
731
732
733
734
735
736
737
738
# File 'lib/rdoc/code_object/class_module.rb', line 728

def search_record
  [
    name,
    full_name,
    full_name,
    '',
    path,
    '',
    snippet(@comment_location),
  ]
end

#search_snippetObject

Returns an HTML snippet of the first comment for search results.



743
744
745
746
747
748
# File 'lib/rdoc/code_object/class_module.rb', line 743

def search_snippet
  first_comment = @comment_location.each_value.first&.first
  return '' unless first_comment && !first_comment.empty?

  snippet(first_comment)
end

#store=(store) ⇒ Object

Sets the store for this class or module and its contained code objects.



765
766
767
768
769
770
771
772
773
# File 'lib/rdoc/code_object/class_module.rb', line 765

def store=(store)
  super

  @attributes .each do |attr|  attr.store  = store end
  @constants  .each do |const| const.store = store end
  @includes   .each do |incl|  incl.store  = store end
  @extends    .each do |ext|   ext.store   = store end
  @method_list.each do |meth|  meth.store  = store end
end

#super_classesObject

Get all super classes of this class in an array. The last element might be a string if the name is unknown.



808
809
810
811
812
813
814
815
816
# File 'lib/rdoc/code_object/class_module.rb', line 808

def super_classes
  result = []
  parent = self
  while parent = parent.superclass
    result << parent
    return result if parent.is_a?(String)
  end
  result
end

#superclassObject

Get the superclass of this class. Attempts to retrieve the superclass object, returns the name if it is not known.



779
780
781
# File 'lib/rdoc/code_object/class_module.rb', line 779

def superclass
  @store.find_class_named(@superclass) || @superclass
end

#superclass=(superclass) ⇒ Object

Set the superclass of this class to superclass

where superclass is one of:

  • nil

  • a String containing the full name of the superclass

  • the RDoc::ClassModule representing the superclass

Raises:

  • (NoMethodError)


792
793
794
795
796
797
798
799
800
801
802
# File 'lib/rdoc/code_object/class_module.rb', line 792

def superclass=(superclass)
  raise NoMethodError, "#{full_name} is a module" if module?
  case superclass
  when RDoc::ClassModule
    @superclass = superclass.full_name
  when nil, String
    @superclass = superclass
  else
    raise TypeError, "superclass must be a String or RDoc::ClassModule, not #{superclass.class}"
  end
end

#to_sObject

:nodoc:



818
819
820
821
822
823
824
# File 'lib/rdoc/code_object/class_module.rb', line 818

def to_s # :nodoc:
  if is_alias_for then
    "#{self.class.name} #{self.full_name} -> #{is_alias_for}"
  else
    super
  end
end

#typeObject

‘module’ or ‘class’



829
830
831
# File 'lib/rdoc/code_object/class_module.rb', line 829

def type
  module? ? 'module' : 'class'
end

#update_aliasesObject

Updates the child modules & classes by replacing the ones that are aliases through a constant.

The aliased module/class is replaced in the children and in RDoc::Store#modules_hash or RDoc::Store#classes_hash by a copy that has RDoc::ClassModule#is_alias_for set to the aliased module/class, and this copy is added to #aliases of the aliased module/class.

Formatters can use the #non_aliases method to retrieve children that are not aliases, for instance to list the namespace content, since the aliased modules are included in the constants of the class/module, that are listed separately.



848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
# File 'lib/rdoc/code_object/class_module.rb', line 848

def update_aliases
  constants.each do |const|
    cm = const.is_alias_for
    cm ||= const.resolved_alias_target if const.is_a?(RDoc::Constant)
    next unless cm

    # Resolve chained aliases (A = B = C) to the real class/module.
    cm = @store.find_class_or_module(cm.full_name) || cm
    while (target = cm.is_alias_for)
      cm = target
    end

    cm_alias = cm.dup
    cm_alias.name = const.name

    if full_name == 'Object'
      # Don't move top-level aliases under Object, they look ugly there
      cm_alias.parent = top_level
    else
      cm_alias.parent = self
    end
    cm_alias.full_name = nil # force update for new parent

    # Don't clobber a real (non-alias) class/module already living at this
    # name. Mirrors the BasicObject = BlankSlate guard in
    # Context#add_module_alias. Existing alias copies (set by
    # add_module_alias or a previous update_aliases pass) carry is_alias_for,
    # so they're still overwritable here.
    existing = @store.find_class_or_module(cm_alias.full_name)
    next if existing && !existing.is_alias_for

    # Persist a lazy-resolved target so Stats#report_constants and
    # Constant#marshal_dump observe the alias relationship. Skipped
    # aliases (above) intentionally leave the constant unmarked.
    const.is_alias_for ||= cm

    cm_alias.aliases.clear
    cm_alias.is_alias_for = cm

    if cm.module? then
      @store.modules_hash[cm_alias.full_name] = cm_alias
      modules_hash[const.name] = cm_alias
    else
      @store.classes_hash[cm_alias.full_name] = cm_alias
      classes_hash[const.name] = cm_alias
    end

    cm.aliases << cm_alias
  end
end

#update_extendsObject

Deletes from #extends those whose module has been removed from the documentation. – FIXME: like update_includes, extends are not reliably removed



920
921
922
923
924
925
926
927
928
# File 'lib/rdoc/code_object/class_module.rb', line 920

def update_extends
  extends.reject! do |ext|
    mod = ext.module

    !(String === mod) && @store.modules_hash[mod.full_name].nil?
  end

  extends.uniq!
end

#update_includesObject

Deletes from #includes those whose module has been removed from the documentation. – FIXME: includes are not reliably removed, see _possible_bug test case



905
906
907
908
909
910
911
912
# File 'lib/rdoc/code_object/class_module.rb', line 905

def update_includes
  includes.reject! do |include|
    mod = include.module
    !(String === mod) && @store.modules_hash[mod.full_name].nil?
  end

  includes.uniq!
end