A Context is something that can hold modules, classes, methods, attributes, aliases, requires, and includes. Classes, modules, and files are all Contexts.
Types of methods
Method visibilities
Creates an unnamed empty context with public visibility
# File lib/rdoc/context.rb, line 174
174: def initialize
175: super
176:
177: @in_files = []
178:
179: @name ||= "unknown"
180: @comment ||= ""
181: @parent = nil
182: @visibility = :public
183:
184: @current_section = Section.new self, nil, nil
185: @sections = [@current_section]
186:
187: initialize_methods_etc
188: initialize_classes_and_modules
189: end
Contexts are sorted by full_name
# File lib/rdoc/context.rb, line 218
218: def <=>(other)
219: full_name <=> other.full_name
220: end
Adds an_alias that is automatically resolved to it’s corresponding RDoc::AnyMethod object.
# File lib/rdoc/context.rb, line 226
226: def add_alias an_alias
227: old_name = an_alias.old_name
228:
229: meth = if an_alias.singleton then
230: find_class_method_named old_name
231: else
232: find_instance_method_named old_name
233: end
234:
235: if meth then
236: add_alias_impl an_alias, meth
237: else
238: add_to @aliases, an_alias
239: unmatched_alias_list = @unmatched_alias_lists[old_name] ||= []
240: unmatched_alias_list.push an_alias
241: end
242:
243: an_alias
244: end
Turns an_alias into an AnyMethod that points to meth
# File lib/rdoc/context.rb, line 249
249: def add_alias_impl(an_alias, meth)
250: new_meth = RDoc::AnyMethod.new an_alias.text, an_alias.new_name
251: new_meth.is_alias_for = meth
252: new_meth.singleton = meth.singleton
253: new_meth.params = meth.params
254:
255: new_meth.comment = an_alias.comment
256:
257: meth.add_alias new_meth
258:
259: add_method new_meth
260:
261: # aliases don't use ongoing visibility
262: new_meth.visibility = meth.visibility
263:
264: new_meth
265: end
Adds attribute
# File lib/rdoc/context.rb, line 270
270: def add_attribute(attribute)
271: add_to @attributes, attribute
272: end
Adds a class named name with superclass.
Given class Container::Item RDoc assumes Container is a module unless it later sees class Container. add_class automatically upgrades name to a class in this case.
# File lib/rdoc/context.rb, line 281
281: def add_class(class_type, name, superclass = 'Object')
282: klass = add_class_or_module @classes, class_type, name, superclass
283:
284: existing = klass.superclass
285: existing = existing.name if existing and not String === existing
286:
287: if superclass != existing and superclass != 'Object' then
288: klass.superclass = superclass
289: end
290:
291: # If the parser encounters Container::Item before encountering
292: # Container, then it assumes that Container is a module. This may not
293: # be the case, so remove Container from the module list if present and
294: # transfer any contained classes and modules to the new class.
295:
296: mod = RDoc::TopLevel.modules_hash.delete klass.full_name
297:
298: if mod then
299: klass.classes_hash.update mod.classes_hash
300: klass.modules_hash.update mod.modules_hash
301: klass.method_list.concat mod.method_list
302:
303: @modules.delete klass.name
304: end
305:
306: RDoc::TopLevel.classes_hash[klass.full_name] = klass
307:
308: klass
309: end
Instantiates a class_type named name and adds it the modules or classes Hash collection.
# File lib/rdoc/context.rb, line 315
315: def add_class_or_module(collection, class_type, name, superclass = nil)
316: full_name = child_name name
317:
318: mod = collection[name]
319:
320: if mod then
321: mod.superclass = superclass unless mod.module?
322: else
323: all = if class_type == RDoc::NormalModule then
324: RDoc::TopLevel.modules_hash
325: else
326: RDoc::TopLevel.classes_hash
327: end
328:
329: mod = all[full_name]
330:
331: unless mod then
332: mod = class_type.new name, superclass
333: else
334: # If the class has been encountered already, check that its
335: # superclass has been set (it may not have been, depending on the
336: # context in which it was encountered).
337: if class_type == RDoc::NormalClass then
338: mod.superclass = superclass unless mod.superclass
339: end
340: end
341:
342: unless @done_documenting then
343: all[full_name] = mod
344: collection[name] = mod
345: end
346:
347: mod.section = @current_section
348: mod.parent = self
349: end
350:
351: mod
352: end
Adds constant
# File lib/rdoc/context.rb, line 357
357: def add_constant(constant)
358: add_to @constants, constant
359: end
Adds included module include
# File lib/rdoc/context.rb, line 364
364: def add_include(include)
365: add_to @includes, include
366: end
Adds method
# File lib/rdoc/context.rb, line 371
371: def add_method(method)
372: method.visibility = @visibility
373: add_to @method_list, method
374:
375: unmatched_alias_list = @unmatched_alias_lists[method.name]
376: if unmatched_alias_list then
377: unmatched_alias_list.each do |unmatched_alias|
378: add_alias_impl unmatched_alias, method
379: @aliases.delete unmatched_alias
380: end
381:
382: @unmatched_alias_lists.delete method.name
383: end
384: end
Adds a module named name. If RDoc already knows name is a class then that class is returned instead. See also add_class
# File lib/rdoc/context.rb, line 390
390: def add_module(class_type, name)
391: return @classes[name] if @classes.key? name
392:
393: add_class_or_module @modules, class_type, name, nil
394: end
Adds an alias from from to name
# File lib/rdoc/context.rb, line 399
399: def add_module_alias from, name
400: to_name = child_name name
401:
402: unless @done_documenting then
403: if from.module? then
404: RDoc::TopLevel.modules_hash
405: else
406: RDoc::TopLevel.classes_hash
407: end[to_name] = from
408:
409: if from.module? then
410: @modules
411: else
412: @classes
413: end[name] = from
414: end
415:
416: from
417: end
Adds require to this context’s top level
# File lib/rdoc/context.rb, line 422
422: def add_require(require)
423: if RDoc::TopLevel === self then
424: add_to @requires, require
425: else
426: parent.add_require require
427: end
428: end
Adds thing to the collection array
# File lib/rdoc/context.rb, line 433
433: def add_to(array, thing)
434: array << thing if @document_self and not @done_documenting
435: thing.parent = self
436: thing.section = @current_section
437: end
Creates the full name for a child with name
# File lib/rdoc/context.rb, line 442
442: def child_name name
443: if RDoc::TopLevel === self then
444: name
445: else
446: "#{self.full_name}::#{name}"
447: end
448: end
Array of classes in this context
# File lib/rdoc/context.rb, line 453
453: def classes
454: @classes.values
455: end
All classes and modules in this namespace
# File lib/rdoc/context.rb, line 460
460: def classes_and_modules
461: classes + modules
462: end
Hash of classes keyed by class name
# File lib/rdoc/context.rb, line 467
467: def classes_hash
468: @classes
469: end
Is part of this thing was defined in file?
# File lib/rdoc/context.rb, line 474
474: def defined_in?(file)
475: @in_files.include?(file)
476: end
Iterator for attributes
# File lib/rdoc/context.rb, line 481
481: def each_attribute # :yields: attribute
482: @attributes.each { |a| yield a }
483: end
Iterator for classes and modules
# File lib/rdoc/context.rb, line 488
488: def each_classmodule(&block) # :yields: module
489: classes_and_modules.sort.each(&block)
490: end
Iterator for constants
# File lib/rdoc/context.rb, line 495
495: def each_constant # :yields: constant
496: @constants.each {|c| yield c}
497: end
Iterator for included modules
# File lib/rdoc/context.rb, line 502
502: def each_include # :yields: include
503: @includes.each do |i| yield i end
504: end
Iterator for methods
# File lib/rdoc/context.rb, line 509
509: def each_method # :yields: method
510: @method_list.sort.each {|m| yield m}
511: end
Finds an attribute with name in this context
# File lib/rdoc/context.rb, line 516
516: def find_attribute_named(name)
517: @attributes.find { |m| m.name == name }
518: end
Finds a class method with name in this context
# File lib/rdoc/context.rb, line 523
523: def find_class_method_named(name)
524: @method_list.find { |meth| meth.singleton && meth.name == name }
525: end
Finds a constant with name in this context
# File lib/rdoc/context.rb, line 530
530: def find_constant_named(name)
531: @constants.find {|m| m.name == name}
532: end
Find a module at a higher scope
# File lib/rdoc/context.rb, line 537
537: def find_enclosing_module_named(name)
538: parent && parent.find_module_named(name)
539: end
Finds a file with name in this context
# File lib/rdoc/context.rb, line 544
544: def find_file_named(name)
545: top_level.class.find_file_named(name)
546: end
Finds an instance method with name in this context
# File lib/rdoc/context.rb, line 551
551: def find_instance_method_named(name)
552: @method_list.find { |meth| !meth.singleton && meth.name == name }
553: end
Finds a method, constant, attribute, module or files named symbol in this context
# File lib/rdoc/context.rb, line 559
559: def find_local_symbol(symbol)
560: find_method_named(symbol) or
561: find_constant_named(symbol) or
562: find_attribute_named(symbol) or
563: find_module_named(symbol) or
564: find_file_named(symbol)
565: end
Finds a instance or module method with name in this context
# File lib/rdoc/context.rb, line 570
570: def find_method_named(name)
571: case name
572: when /\A#/ then
573: find_instance_method_named name[1..1]
574: when /\A::/ then
575: find_class_method_named name[2..1]
576: else
577: @method_list.find { |meth| meth.name == name }
578: end
579: end
Find a module with name using ruby’s scoping rules
# File lib/rdoc/context.rb, line 584
584: def find_module_named(name)
585: res = @modules[name] || @classes[name]
586: return res if res
587: return self if self.name == name
588: find_enclosing_module_named name
589: end
Look up symbol. If method is non-nil, then we assume the symbol references a module that contains that method.
# File lib/rdoc/context.rb, line 595
595: def find_symbol(symbol, method = nil)
596: result = nil
597:
598: case symbol
599: when /^::([A-Z].*)/ then
600: result = top_level.find_symbol($1)
601: when /::/ then
602: modules = symbol.split(/::/)
603:
604: unless modules.empty? then
605: module_name = modules.shift
606: result = find_module_named(module_name)
607:
608: if result then
609: modules.each do |name|
610: result = result.find_module_named name
611: break unless result
612: end
613: end
614: end
615: end
616:
617: unless result then
618: # if a method is specified, then we're definitely looking for
619: # a module, otherwise it could be any symbol
620: if method then
621: result = find_module_named symbol
622: else
623: result = find_local_symbol symbol
624: if result.nil? then
625: if symbol =~ /^[A-Z]/ then
626: result = parent
627: while result && result.name != symbol do
628: result = result.parent
629: end
630: end
631: end
632: end
633: end
634:
635: result = result.find_local_symbol method if result and method
636:
637: result
638: end
The full name for this context. This method is overridden by subclasses.
# File lib/rdoc/context.rb, line 643
643: def full_name
644: '(unknown)'
645: end
URL for this with a prefix
# File lib/rdoc/context.rb, line 650
650: def http_url(prefix)
651: path = full_name
652: path = path.gsub(/<<\s*(\w*)/, 'from-\1') if path =~ /<</
653: path = [prefix] + path.split('::')
654:
655: File.join(*path.compact) + '.html'
656: end
Sets the defaults for classes and modules
# File lib/rdoc/context.rb, line 194
194: def initialize_classes_and_modules
195: @classes = {}
196: @modules = {}
197: end
Sets the defaults for methods and so-forth
# File lib/rdoc/context.rb, line 202
202: def initialize_methods_etc
203: @method_list = []
204: @attributes = []
205: @aliases = []
206: @requires = []
207: @includes = []
208: @constants = []
209:
210: # This Hash maps a method name to a list of unmatched aliases (aliases of
211: # a method not yet encountered).
212: @unmatched_alias_lists = {}
213: end
Breaks method_list into a nested hash by type (class or instance) and visibility (public, protected private)
# File lib/rdoc/context.rb, line 662
662: def methods_by_type
663: methods = {}
664:
665: TYPES.each do |type|
666: visibilities = {}
667: VISIBILITIES.each do |vis|
668: visibilities[vis] = []
669: end
670:
671: methods[type] = visibilities
672: end
673:
674: each_method do |method|
675: methods[method.type][method.visibility] << method
676: end
677:
678: methods
679: end
Yields Method and Attr entries matching the list of names in methods. Attributes are only returned when singleton is false.
# File lib/rdoc/context.rb, line 685
685: def methods_matching(methods, singleton = false)
686: count = 0
687:
688: @method_list.each do |m|
689: if methods.include? m.name and m.singleton == singleton then
690: yield m
691: count += 1
692: end
693: end
694:
695: return if count == methods.size || singleton
696:
697: @attributes.each do |a|
698: yield a if methods.include? a.name
699: end
700: end
Array of modules in this context
# File lib/rdoc/context.rb, line 705
705: def modules
706: @modules.values
707: end
Hash of modules keyed by module name
# File lib/rdoc/context.rb, line 712
712: def modules_hash
713: @modules
714: end
Changes the visibility for new methods to visibility
# File lib/rdoc/context.rb, line 719
719: def ongoing_visibility=(visibility)
720: @visibility = visibility
721: end
Record which file top_level is in
# File lib/rdoc/context.rb, line 726
726: def record_location(top_level)
727: @in_files << top_level unless @in_files.include?(top_level)
728: end
Removes classes and modules when we see a :nodoc: all
# File lib/rdoc/context.rb, line 751
751: def remove_classes_and_modules
752: initialize_classes_and_modules
753: end
If a class’s documentation is turned off after we’ve started collecting methods etc., we need to remove the ones we have
# File lib/rdoc/context.rb, line 734
734: def remove_methods_etc
735: initialize_methods_etc
736: end
Creates a new section with title and comment
# File lib/rdoc/context.rb, line 758
758: def set_current_section(title, comment)
759: @current_section = Section.new self, title, comment
760: @sections << @current_section
761: end
Given an array methods of method names, set the visibility of each to visibility
# File lib/rdoc/context.rb, line 742
742: def set_visibility_for(methods, visibility, singleton = false)
743: methods_matching methods, singleton do |m|
744: m.visibility = visibility
745: end
746: end
Return the TopLevel that owns us
# File lib/rdoc/context.rb, line 766
766: def top_level
767: return @top_level if defined? @top_level
768: @top_level = self
769: @top_level = @top_level.parent until RDoc::TopLevel === @top_level
770: @top_level
771: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.