Class: RDoc::Context

Class | RDoc::Context
In: | rdoc/code_objects.rb
Parent: | CodeObject

A Context is something that can hold modules, classes, methods, attributes, aliases, requires, and includes. Classes, modules, and files are all Contexts.

### Methods

<=> add_alias add_attribute add_class add_class_or_module add_constant add_include add_method add_module add_require add_to classes defined_in? each_attribute each_classmodule each_constant each_method find_enclosing_module_named find_local_symbol find_module_named find_symbol initialize_classes_and_modules initialize_methods_etc modules new ongoing_visibility= record_location remove_classes_and_modules remove_methods_etc set_current_section set_visibility_for toplevel

### Classes and Modules

Class RDoc::Context::Section

### Attributes

aliases | [R] |
attributes | [R] |
constants | [R] |
in_files | [R] |
includes | [R] |
method_list | [R] |
name | [R] |
requires | [R] |
sections | [R] |
visibility | [R] |

### Public Class methods

new()

[Source]

    # File rdoc/code_objects.rb, line 163
        def initialize
          super()

          @in_files    = []

          @name    ||= "unknown"
          @comment ||= ""
          @parent  = nil
          @visibility = :public

          @current_section = Section.new(nil, nil)
          @sections = [ @current_section ]

          initialize_methods_etc
          initialize_classes_and_modules
        end

### Public Instance methods

<=>(other)

allow us to sort modules by name

[Source]

    # File rdoc/code_objects.rb, line 370
        def <=>(other)
          name <=> other.name
        end

add_alias(an_alias)

[Source]

    # File rdoc/code_objects.rb, line 247
        def add_alias(an_alias)
          meth = find_instance_method_named(an_alias.old_name)
          if meth
            new_meth = AnyMethod.new(an_alias.text, an_alias.new_name)
            new_meth.is_alias_for = meth
            new_meth.singleton    = meth.singleton
            new_meth.params       = meth.params
            new_meth.comment = "Alias for \##{meth.name}"
            meth.add_alias(new_meth)
            add_method(new_meth)
          else
            add_to(@aliases, an_alias)
          end
        end

add_attribute(an_attribute)

[Source]

    # File rdoc/code_objects.rb, line 243
        def add_attribute(an_attribute)
          add_to(@attributes, an_attribute)
        end

add_class(class_type, name, superclass)

[Source]

    # File rdoc/code_objects.rb, line 229
        def add_class(class_type, name, superclass)
          add_class_or_module(@classes, class_type, name, superclass)
        end

add_class_or_module(collection, class_type, name, superclass=nil)

[Source]

    # File rdoc/code_objects.rb, line 279
        def add_class_or_module(collection, class_type, name, superclass=nil)
          cls = collection[name]
          if cls
            puts "Reusing class/module #{name}" if $DEBUG
          else
            cls = class_type.new(name, superclass)
            puts "Adding class/module #{name} to #@name" if $DEBUG
    #        collection[name] = cls if @document_self  && !@done_documenting
            collection[name] = cls if !@done_documenting
            cls.parent = self
            cls.section = @current_section
          end
          cls
        end

add_constant(const)

[Source]

    # File rdoc/code_objects.rb, line 266
        def add_constant(const)
          add_to(@constants, const)
        end

add_include(an_include)

[Source]

    # File rdoc/code_objects.rb, line 262
        def add_include(an_include)
          add_to(@includes, an_include)
        end

add_method(a_method)

[Source]

    # File rdoc/code_objects.rb, line 237
        def add_method(a_method)
          puts "Adding #@visibility method #{a_method.name} to #@name" if $DEBUG
          a_method.visibility = @visibility
          add_to(@method_list, a_method)
        end

add_module(class_type, name)

[Source]

    # File rdoc/code_objects.rb, line 233
        def add_module(class_type, name)
          add_class_or_module(@modules, class_type, name, nil)
        end

add_require(a_require)

Requires always get added to the top-level (file) context

[Source]

    # File rdoc/code_objects.rb, line 271
        def add_require(a_require)
          if self.kind_of? TopLevel
            add_to(@requires, a_require)
          else
            parent.add_require(a_require)
          end
        end

add_to(array, thing)

[Source]

    # File rdoc/code_objects.rb, line 294
        def add_to(array, thing)
          array <<  thing if @document_self  && !@done_documenting
          thing.parent = self
          thing.section = @current_section
        end

classes()

map the class hash to an array externally

[Source]

    # File rdoc/code_objects.rb, line 181
        def classes
          @classes.values
        end

defined_in?(file)

Return true if at least part of this thing was defined in `file`

[Source]

    # File rdoc/code_objects.rb, line 225
        def defined_in?(file)
          @in_files.include?(file)
        end

each_attribute() {|a| ...}

[Source]

    # File rdoc/code_objects.rb, line 352
        def each_attribute
          @attributes.each {|a| yield a}
        end

each_classmodule() {|m| ...}

Iterate over all the classes and modules in this object

[Source]

    # File rdoc/code_objects.rb, line 343
        def each_classmodule
          @modules.each_value {|m| yield m}
          @classes.each_value {|c| yield c}
        end

each_constant() {|c| ...}

[Source]

    # File rdoc/code_objects.rb, line 356
        def each_constant
          @constants.each {|c| yield c}
        end

each_method() {|m| ...}

[Source]

    # File rdoc/code_objects.rb, line 348
        def each_method
          @method_list.each {|m| yield m}
        end

find_enclosing_module_named(name)

find a module at a higher scope

[Source]

    # File rdoc/code_objects.rb, line 336
        def find_enclosing_module_named(name)
          parent && parent.find_module_named(name)
        end

find_local_symbol(symbol)

[Source]

    # File rdoc/code_objects.rb, line 422
        def find_local_symbol(symbol)
          res = find_method_named(symbol) ||
                find_constant_named(symbol) ||
                find_attribute_named(symbol) ||
                find_module_named(symbol)
        end

find_module_named(name)

Find a named module

[Source]

    # File rdoc/code_objects.rb, line 328
        def find_module_named(name)
          return self if self.name == name
          res = @modules[name] || @classes[name]
          return res if res
          find_enclosing_module_named(name)
        end

find_symbol(symbol, method=nil)

Look up the given symbol. If method is non-nil, then we assume the symbol references a module that contains that method

[Source]

    # File rdoc/code_objects.rb, line 377
        def find_symbol(symbol, method=nil)
          result = nil
          case symbol
          when /^::(.*)/
            result = toplevel.find_symbol($1)
          when /::/
            modules = symbol.split(/::/)
            unless modules.empty?
              module_name = modules.shift
              result = find_module_named(module_name)
              if result
                modules.each do |module_name|
                  result = result.find_module_named(module_name)
                  break unless result
                end
              end
            end
          else
            # if a method is specified, then we're definitely looking for
            # a module, otherwise it could be any symbol
            if method
              result = find_module_named(symbol)
            else
              result = find_local_symbol(symbol)
              if result.nil?
                if symbol =~ /^[A-Z]/
                  result = parent
                  while result && result.name != symbol
                    result = result.parent
                  end
                end
              end
            end
          end
          if result && method
            if !result.respond_to?(:find_local_symbol)
              p result.name
              p method
              fail
            end
            result = result.find_local_symbol(method)
          end
          result
        end

initialize_classes_and_modules()

[Source]

    # File rdoc/code_objects.rb, line 322
        def initialize_classes_and_modules
          @classes     = {}
          @modules     = {}
        end

initialize_methods_etc()

[Source]

    # File rdoc/code_objects.rb, line 308
        def initialize_methods_etc
          @method_list = []
          @attributes  = []
          @aliases     = []
          @requires    = []
          @includes    = []
          @constants   = []
        end

modules()

map the module hash to an array externally

[Source]

    # File rdoc/code_objects.rb, line 186
        def modules
          @modules.values
        end

ongoing_visibility=(vis)

Change the default visibility for new methods

[Source]

    # File rdoc/code_objects.rb, line 191
        def ongoing_visibility=(vis)
          @visibility = vis
        end

record_location(toplevel)

Record the file that we happen to find it in

[Source]

    # File rdoc/code_objects.rb, line 220
        def record_location(toplevel)
          @in_files << toplevel unless @in_files.include?(toplevel)
        end

remove_classes_and_modules()

and remove classes and modules when we see a :nodoc: all

[Source]

    # File rdoc/code_objects.rb, line 318
        def remove_classes_and_modules
          initialize_classes_and_modules
        end

remove_methods_etc()

If a class’s documentation is turned off after we’ve started collecting methods etc., we need to remove the ones we have

[Source]

    # File rdoc/code_objects.rb, line 304
        def remove_methods_etc
          initialize_methods_etc
        end

set_current_section(title, comment)

Handle sections

[Source]

    # File rdoc/code_objects.rb, line 431
        def set_current_section(title, comment)
          @current_section = Section.new(title, comment)
          @sections << @current_section
        end

set_visibility_for(methods, vis, singleton=false)

Given an array `methods` of method names, set the visibility of the corresponding AnyMethod object

[Source]

    # File rdoc/code_objects.rb, line 198
        def set_visibility_for(methods, vis, singleton=false)
          count = 0
          @method_list.each do |m|
            if methods.include?(m.name) && m.singleton == singleton
              m.visibility = vis
              count += 1
            end
          end

          return if count == methods.size || singleton

          # perhaps we need to look at attributes

          @attributes.each do |a|
            if methods.include?(a.name)
              a.visibility = vis
              count += 1
            end
          end
        end

toplevel()

Return the toplevel that owns us

[Source]

    # File rdoc/code_objects.rb, line 362
        def toplevel
          return @toplevel if defined? @toplevel
          @toplevel = self
          @toplevel = @toplevel.parent until TopLevel === @toplevel
          @toplevel
        end

[Validate]