Class: RDoc::ClassModule

Inherits:
Context show all
Defined in:
lib/rdoc/class_module.rb

Overview

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

Direct Known Subclasses

AnonClass, 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

1

Constants inherited from Context

RDoc::Context::TYPES

Constants included from Text

Text::TO_HTML_CHARACTERS

Instance Attribute Summary collapse

Attributes inherited from Context

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

Attributes inherited from CodeObject

#comment, #document_children, #document_self, #done_documenting, #file, #force_documentation, #line, #metadata, #offset, #parent, #received_nodoc, #section, #viewer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Context

#<=>, #add_alias, #add_attribute, #add_class, #add_class_or_module, #add_constant, #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, #defined_in?, #display, #each_attribute, #each_classmodule, #each_constant, #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, #fully_documented?, #http_url, #initialize_methods_etc, #instance_attributes, #instance_method_list, #methods_by_type, #methods_matching, #modules, #modules_hash, #ongoing_visibility=, #record_location, #remove_from_documentation?, #remove_invisible, #remove_invisible_in, #resolve_aliases, #sections, #sections_hash, #set_current_section, #set_visibility_for, #top_level, #upgrade_to_class

Methods included from Generator::Markup

#aref_to, #as_href, #cvs_url, #description, #formatter

Methods inherited from CodeObject

#display?, #documented?, #each_parent, #file_name, #full_name=, #ignore, #ignored?, #parent_file_name, #parent_name, #record_location, #start_doc, #stop_doc

Methods included from Text

encode_fallback, #expand_tabs, #flush_left, #markup, #normalize_comment, #strip_hashes, #strip_newlines, #strip_stars, #to_html, #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.



101
102
103
104
105
106
107
108
109
110
# File 'lib/rdoc/class_module.rb', line 101

def initialize(name, superclass = nil)
  @constant_aliases = []
  @diagram          = nil
  @is_alias_for     = nil
  @name             = name
  @superclass       = superclass
  @comment_location = [] # [[comment, location]]

  super()
end

Instance Attribute Details

#comment_locationObject (readonly)

Comment and the location it came from. Use #add_comment to add comments



27
28
29
# File 'lib/rdoc/class_module.rb', line 27

def comment_location
  @comment_location
end

#constant_aliasesObject

Constants that are aliases for this class or module



22
23
24
# File 'lib/rdoc/class_module.rb', line 22

def constant_aliases
  @constant_aliases
end

#diagramObject

:nodoc:



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

def diagram
  @diagram
end

#is_alias_forObject

Class or module this constant is an alias for



34
35
36
# File 'lib/rdoc/class_module.rb', line 34

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)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/rdoc/class_module.rb', line 42

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

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

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

  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.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.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.



117
118
119
120
121
122
123
124
125
126
# File 'lib/rdoc/class_module.rb', line 117

def add_comment comment, location
  return if comment.empty? or not document_self

  original = comment

  comment = normalize_comment comment
  @comment_location << [comment, location]

  self.comment = original
end

#ancestorsObject

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.



140
141
142
# File 'lib/rdoc/class_module.rb', line 140

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

#clear_commentObject

Clears the comment. Used by the ruby parser.



147
148
149
# File 'lib/rdoc/class_module.rb', line 147

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 +=.



157
158
159
160
161
162
163
164
# File 'lib/rdoc/class_module.rb', line 157

def comment= comment
  return if comment.empty?

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

  super comment
end

#complete(min_visibility) ⇒ Object

Prepares this ClassModule for use by a generator.

See RDoc::TopLevel::complete



171
172
173
174
175
176
# File 'lib/rdoc/class_module.rb', line 171

def complete min_visibility
  update_aliases
  remove_nodoc_children
  update_includes
  remove_invisible min_visibility
end

#each_ancestorObject

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



182
183
184
185
186
187
# File 'lib/rdoc/class_module.rb', line 182

def each_ancestor # :yields: module
  ancestors.each do |mod|
    next if String === mod
    yield mod
  end
end

#find_ancestor_local_symbol(symbol) ⇒ Object

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



192
193
194
195
196
197
198
199
# File 'lib/rdoc/class_module.rb', line 192

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



204
205
206
207
208
209
210
211
212
# File 'lib/rdoc/class_module.rb', line 204

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



217
218
219
220
221
222
223
# File 'lib/rdoc/class_module.rb', line 217

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

#marshal_dumpObject

TODO: filter included items by #display?



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rdoc/class_module.rb', line 228

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

  method_types = methods_by_type.map do |type, visibilities|
    visibilities = visibilities.map do |visibility, methods|
      method_names = methods.map do |method|
        [method.name, method.file_name]
      end

      [visibility, method_names.uniq]
    end

    [type, visibilities]
  end

  [ MARSHAL_VERSION,
    @name,
    full_name,
    @superclass,
    parse(@comment_location),
    attrs,
    constants.map do |const|
      [const.name, parse(const.comment), const.file_name]
    end,
    includes.map do |incl|
      [incl.name, parse(incl.comment), incl.file_name]
    end,
    method_types,
  ]
end

#marshal_load(array) ⇒ Object

:nodoc:



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/rdoc/class_module.rb', line 263

def marshal_load array # :nodoc:
  initialize_methods_etc
  @current_section   = nil
  @document_self     = true
  @done_documenting  = false
  @parent            = nil
  @temporary_section = nil
  @visibility        = nil

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

  @comment_location = if RDoc::Markup::Document === @comment.parts.first then
                        @comment
                      else
                        RDoc::Markup::Document.new @comment
                      end

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

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

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

  array[6].each do |name, comment, file|
    const = add_constant RDoc::Constant.new(name, nil, comment)
    const.record_location RDoc::TopLevel.new file
  end

  array[7].each do |name, comment, file|
    incl = add_include RDoc::Include.new(name, comment)
    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 nil, name
        method.singleton = true if type == 'class'
        method.record_location RDoc::TopLevel.new file
        add_method method
      end
    end
  end
end

#merge(class_module) ⇒ Object

Merges class_module into this ClassModule.

The data in class_module is preferred over the receiver.



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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
# File 'lib/rdoc/class_module.rb', line 323

def merge class_module
  other_document = parse class_module.comment_location

  if other_document then
    document = parse @comment_location

    document = document.merge other_document

    @comment = @comment_location = document
  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

  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

  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


390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/rdoc/class_module.rb', line 390

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 }

  my_things.delete_if do |file, things|
    next false unless other_files.include? file

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

    true
  end

  other_things.each do |file, things|
    my_things[file].each { |thing| yield false, thing } if
      my_things.include?(file)

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

#module?Boolean

Does this object represent a module?

Returns:

  • (Boolean)


417
418
419
# File 'lib/rdoc/class_module.rb', line 417

def module?
  false
end

#name=(new_name) ⇒ Object

Allows overriding the initial name.

Used for modules and classes that are constant aliases.



426
427
428
# File 'lib/rdoc/class_module.rb', line 426

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.



465
466
467
# File 'lib/rdoc/class_module.rb', line 465

def name_for_path
  is_alias_for ? is_alias_for.full_name : full_name
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).



474
475
476
# File 'lib/rdoc/class_module.rb', line 474

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.



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/rdoc/class_module.rb', line 434

def parse comment_location
  case comment_location
  when String then
    super
  when Array then
    docs = comment_location.map do |comment, location|
      doc = super comment
      doc.file = location.absolute_name
      doc
    end

    RDoc::Markup::Document.new(*docs)
  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



456
457
458
# File 'lib/rdoc/class_module.rb', line 456

def path
  http_url RDoc::RDoc.current.generator.class_dir
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.



486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/rdoc/class_module.rb', line 486

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

  modules_hash.each_key do |name|
    full_name = prefix + name
    modules_hash.delete name unless RDoc::TopLevel.all_modules_hash[full_name]
  end

  classes_hash.each_key do |name|
    full_name = prefix + name
    classes_hash.delete name unless RDoc::TopLevel.all_classes_hash[full_name]
  end
end

#superclassObject

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



504
505
506
# File 'lib/rdoc/class_module.rb', line 504

def superclass
  RDoc::TopLevel.find_class_named(@superclass) || @superclass
end

#superclass=(superclass) ⇒ Object

Set the superclass of this class to superclass

Raises:

  • (NoMethodError)


511
512
513
514
# File 'lib/rdoc/class_module.rb', line 511

def superclass=(superclass)
  raise NoMethodError, "#{full_name} is a module" if module?
  @superclass = superclass
end

#to_sObject

:nodoc:



516
517
518
519
520
521
522
# File 'lib/rdoc/class_module.rb', line 516

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'



527
528
529
# File 'lib/rdoc/class_module.rb', line 527

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::TopLevel::all_modules_hash or RDoc::TopLevel::all_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.



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/rdoc/class_module.rb', line 546

def update_aliases
  constants.each do |const|
    next unless cm = const.is_alias_for
    cm_alias = cm.dup
    cm_alias.name = const.name
    cm_alias.parent = self
    cm_alias.full_name = nil  # force update for new parent
    cm_alias.aliases.clear
    cm_alias.is_alias_for = cm

    if cm.module? then
      RDoc::TopLevel.all_modules_hash[cm_alias.full_name] = cm_alias
      modules_hash[const.name] = cm_alias
    else
      RDoc::TopLevel.all_classes_hash[cm_alias.full_name] = cm_alias
      classes_hash[const.name] = cm_alias
    end

    cm.aliases << cm_alias
  end
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



574
575
576
577
578
579
# File 'lib/rdoc/class_module.rb', line 574

def update_includes
  includes.reject! do |include|
    mod = include.module
    !(String === mod) && RDoc::TopLevel.all_modules_hash[mod.full_name].nil?
  end
end