class RDoc::Context

Context 是一个可以容纳模块、类、方法、属性、别名、requires 和 includes 的东西。 类、模块和文件都是 Context。

常量

TYPES

方法类型

属性

aliases[R]

类/模块别名

attributes[R]

所有 attr* 方法

block_params[RW]

在此上下文中解析的下一个 MethodAttr 中要使用的块参数

constants[R]

定义的常量

constants_hash[R]

已注册常量的哈希表。

current_line_visibility[W]

此行当前的可见性

current_section[W]

设置文档的当前文档部分

extends[R]

此上下文扩展的模块

external_aliases[R]

无法解析的别名。

in_files[R]

找到此上下文的文件

includes[R]

此上下文包含的模块

method_list[R]

在此上下文中定义的方法

methods_hash[R]

已注册方法的哈希表。属性也会在此处注册,如果是 RW 则注册两次。

name[R]

此类的名称,不包括命名空间。另请参阅 full_name

params[RW]

在此上下文中解析的下一个 MethodAttr 中要使用的参数

requires[R]

此上下文需要的文件

temporary_section[RW]

为接下来添加的方法、属性或常量使用此部分。

unmatched_alias_lists[RW]

哈希表 old_name => [aliases],用于尚未解析为方法/属性的别名。(不要与上下文的别名混淆。)

visibility[RW]

此上下文的当前可见性

公共类方法

new() 单击以切换源

创建一个具有公共当前可见性的未命名空上下文

调用超类方法 RDoc::CodeObject::new
# File rdoc/code_object/context.rb, line 123
def initialize
  super

  @in_files = []

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

  @current_section = Section.new self, nil, nil
  @sections = { nil => @current_section }
  @temporary_section = nil

  @classes = {}
  @modules = {}

  initialize_methods_etc
end

公共实例方法

<=>(other) 单击以切换源

上下文按 full_name 排序

# File rdoc/code_object/context.rb, line 171
def <=>(other)
  return nil unless RDoc::CodeObject === other

  full_name <=> other.full_name
end
add(klass, name, comment) 单击以切换源

将类型为 klass、具有给定 namecomment 的项添加到上下文中。

目前仅支持 RDoc::ExtendRDoc::Include

# File rdoc/code_object/context.rb, line 183
def add klass, name, comment
  if RDoc::Extend == klass then
    ext = RDoc::Extend.new name, comment
    add_extend ext
  elsif RDoc::Include == klass then
    incl = RDoc::Include.new name, comment
    add_include incl
  else
    raise NotImplementedError, "adding a #{klass} is not implemented"
  end
end
add_alias(an_alias) 单击以切换源

添加自动解析的 an_alias

# File rdoc/code_object/context.rb, line 198
def add_alias an_alias
  return an_alias unless @document_self

  method_attr = find_method(an_alias.old_name, an_alias.singleton) ||
                find_attribute(an_alias.old_name, an_alias.singleton)

  if method_attr then
    method_attr.add_alias an_alias, self
  else
    add_to @external_aliases, an_alias
    unmatched_alias_list =
      @unmatched_alias_lists[an_alias.pretty_old_name] ||= []
    unmatched_alias_list.push an_alias
  end

  an_alias
end
add_attribute(attribute) 单击以切换源

如果 attribute 不存在,则添加它。如果存在(作为方法或属性),则在注释为空时更新注释。

仅当属性定义新方法时才注册。例如,如果方法 foo 存在,则不会注册 attr_reader :foo,但如果方法 foo 存在,而 foo= 不存在,则会注册 attr_accessor :foo

# File rdoc/code_object/context.rb, line 225
def add_attribute attribute
  return attribute unless @document_self

  # mainly to check for redefinition of an attribute as a method
  # TODO find a policy for 'attr_reader :foo' + 'def foo=()'
  register = false

  key = nil

  if attribute.rw.index 'R' then
    key = attribute.pretty_name
    known = @methods_hash[key]

    if known then
      known.comment = attribute.comment if known.comment.empty?
    elsif registered = @methods_hash[attribute.pretty_name + '='] and
          RDoc::Attr === registered then
      registered.rw = 'RW'
    else
      @methods_hash[key] = attribute
      register = true
    end
  end

  if attribute.rw.index 'W' then
    key = attribute.pretty_name + '='
    known = @methods_hash[key]

    if known then
      known.comment = attribute.comment if known.comment.empty?
    elsif registered = @methods_hash[attribute.pretty_name] and
          RDoc::Attr === registered then
      registered.rw = 'RW'
    else
      @methods_hash[key] = attribute
      register = true
    end
  end

  if register then
    attribute.visibility = @visibility
    add_to @attributes, attribute
    resolve_aliases attribute
  end

  attribute
end
add_class(class_type, given_name, superclass = '::Object') 单击以切换源

添加名为 given_name 的类,具有 superclass

given_namesuperclass 都可能包含 '::',并且相对于 self 上下文进行解释。这允许正确处理如下示例

class RDoc::Gauntlet < Gauntlet
module Mod
  class Object   # implies < ::Object
  class SubObject < Object  # this is _not_ ::Object

给定 class Container::ItemRDoc 假定 Container 是一个模块,除非稍后看到 class Container。在这种情况下,add_class 会自动将 given_name 升级为类。

# File rdoc/code_object/context.rb, line 288
def add_class class_type, given_name, superclass = '::Object'
  # superclass +nil+ is passed by the C parser in the following cases:
  # - registering Object in 1.8 (correct)
  # - registering BasicObject in 1.9 (correct)
  # - registering RubyVM in 1.9 in iseq.c (incorrect: < Object in vm.c)
  #
  # If we later find a superclass for a registered class with a nil
  # superclass, we must honor it.

  # find the name & enclosing context
  if given_name =~ /^:+(\w+)$/ then
    full_name = $1
    enclosing = top_level
    name = full_name.split(/:+/).last
  else
    full_name = child_name given_name

    if full_name =~ /^(.+)::(\w+)$/ then
      name = $2
      ename = $1
      enclosing = @store.classes_hash[ename] || @store.modules_hash[ename]
      # HACK: crashes in actionpack/lib/action_view/helpers/form_helper.rb (metaprogramming)
      unless enclosing then
        # try the given name at top level (will work for the above example)
        enclosing = @store.classes_hash[given_name] ||
                    @store.modules_hash[given_name]
        return enclosing if enclosing
        # not found: create the parent(s)
        names = ename.split('::')
        enclosing = self
        names.each do |n|
          enclosing = enclosing.classes_hash[n] ||
                      enclosing.modules_hash[n] ||
                      enclosing.add_module(RDoc::NormalModule, n)
        end
      end
    else
      name = full_name
      enclosing = self
    end
  end

  # fix up superclass
  if full_name == 'BasicObject' then
    superclass = nil
  elsif full_name == 'Object' then
    superclass = '::BasicObject'
  end

  # find the superclass full name
  if superclass then
    if superclass =~ /^:+/ then
      superclass = $' #'
    else
      if superclass =~ /^(\w+):+(.+)$/ then
        suffix = $2
        mod = find_module_named($1)
        superclass = mod.full_name + '::' + suffix if mod
      else
        mod = find_module_named(superclass)
        superclass = mod.full_name if mod
      end
    end

    # did we believe it was a module?
    mod = @store.modules_hash.delete superclass

    upgrade_to_class mod, RDoc::NormalClass, mod.parent if mod

    # e.g., Object < Object
    superclass = nil if superclass == full_name
  end

  klass = @store.classes_hash[full_name]

  if klass then
    # if TopLevel, it may not be registered in the classes:
    enclosing.classes_hash[name] = klass

    # update the superclass if needed
    if superclass then
      existing = klass.superclass
      existing = existing.full_name unless existing.is_a?(String) if existing
      if existing.nil? ||
         (existing == 'Object' && superclass != 'Object') then
        klass.superclass = superclass
      end
    end
  else
    # this is a new class
    mod = @store.modules_hash.delete full_name

    if mod then
      klass = upgrade_to_class mod, RDoc::NormalClass, enclosing

      klass.superclass = superclass unless superclass.nil?
    else
      klass = class_type.new name, superclass

      enclosing.add_class_or_module(klass, enclosing.classes_hash,
                                    @store.classes_hash)
    end
  end

  klass.parent = self

  klass
end
add_class_or_module(mod, self_hash, all_hash) 单击以切换源

将类或模块 mod 添加到模块或类哈希表 self_hash,以及 all_hash (TopLevel::modules_hashTopLevel::classes_hash),除非 done_documentingtrue。将 modparent 设置为 self,将其 section 设置为 current_section。返回 mod

# File rdoc/code_object/context.rb, line 404
def add_class_or_module mod, self_hash, all_hash
  mod.section = current_section # TODO declaring context? something is
                                # wrong here...
  mod.parent = self
  mod.full_name = nil
  mod.store = @store

  unless @done_documenting then
    self_hash[mod.name] = mod
    # this must be done AFTER adding mod to its parent, so that the full
    # name is correct:
    all_hash[mod.full_name] = mod
    if @store.unmatched_constant_alias[mod.full_name] then
      to, file = @store.unmatched_constant_alias[mod.full_name]
      add_module_alias mod, mod.name, to, file
    end
  end

  mod
end
add_constant(constant) 单击以切换源

如果 constant 不存在,则添加它。如果存在,则在注释、值和/或 is_alias_for 为空/nil 时更新已知的常量。

# File rdoc/code_object/context.rb, line 429
def add_constant constant
  return constant unless @document_self

  # HACK: avoid duplicate 'PI' & 'E' in math.c (1.8.7 source code)
  # (this is a #ifdef: should be handled by the C parser)
  known = @constants_hash[constant.name]

  if known then
    known.comment = constant.comment if known.comment.empty?

    known.value = constant.value if
      known.value.nil? or known.value.strip.empty?

    known.is_alias_for ||= constant.is_alias_for
  else
    @constants_hash[constant.name] = constant
    add_to @constants, constant
  end

  constant
end
add_extend(ext) 单击以切换源

添加扩展模块 ext,它应该是 RDoc::Extend

# File rdoc/code_object/context.rb, line 463
def add_extend ext
  add_to @extends, ext

  ext
end
add_include(include) 单击以切换源

添加包含的模块 include,它应该是 RDoc::Include

# File rdoc/code_object/context.rb, line 454
def add_include include
  add_to @includes, include

  include
end
add_method(method) 单击以切换源

如果 method 不存在,则添加它。如果存在(作为方法或属性),则在注释为空时更新注释。

# File rdoc/code_object/context.rb, line 473
def add_method method
  return method unless @document_self

  # HACK: avoid duplicate 'new' in io.c & struct.c (1.8.7 source code)
  key = method.pretty_name
  known = @methods_hash[key]

  if known then
    if @store then # otherwise we are loading
      known.comment = method.comment if known.comment.empty?
      previously = ", previously in #{known.file}" unless
        method.file == known.file
      @store.rdoc.options.warn \
        "Duplicate method #{known.full_name} in #{method.file}#{previously}"
    end
  else
    @methods_hash[key] = method
    if @current_line_visibility
      method.visibility, @current_line_visibility = @current_line_visibility, nil
    else
      method.visibility = @visibility
    end
    add_to @method_list, method
    resolve_aliases method
  end

  method
end
add_module(class_type, name) 单击以切换源

添加名为 name 的模块。如果 RDoc 已经知道 name 是一个类,则会返回该类。另请参阅 add_class

# File rdoc/code_object/context.rb, line 506
def add_module(class_type, name)
  mod = @classes[name] || @modules[name]
  return mod if mod

  full_name = child_name name
  mod = @store.modules_hash[full_name] || class_type.new(name)

  add_class_or_module mod, @modules, @store.modules_hash
end
add_module_alias(from, from_name, to, file) 单击以切换源

添加从 from(类或模块)到在 file 中定义的 name 的别名。

# File rdoc/code_object/context.rb, line 527
def add_module_alias from, from_name, to, file
  return from if @done_documenting

  to_full_name = child_name to.name

  # if we already know this name, don't register an alias:
  # see the metaprogramming in lib/active_support/basic_object.rb,
  # where we already know BasicObject is a class when we find
  # BasicObject = BlankSlate
  return from if @store.find_class_or_module to_full_name

  unless from
    @store.unmatched_constant_alias[child_name(from_name)] = [to, file]
    return to
  end

  new_to = from.dup
  new_to.name = to.name
  new_to.full_name = nil

  if new_to.module? then
    @store.modules_hash[to_full_name] = new_to
    @modules[to.name] = new_to
  else
    @store.classes_hash[to_full_name] = new_to
    @classes[to.name] = new_to
  end

  # Registers a constant for this alias.  The constant value and comment
  # will be updated later, when the Ruby parser adds the constant
  const = RDoc::Constant.new to.name, nil, new_to.comment
  const.record_location file
  const.is_alias_for = from
  add_constant const

  new_to
end
add_module_by_normal_module(mod) 单击以切换源

通过 RDoc::NormalModule 实例添加模块。另请参阅 add_module

# File rdoc/code_object/context.rb, line 519
def add_module_by_normal_module(mod)
  add_class_or_module mod, @modules, @store.modules_hash
end
add_require(require) 单击以切换源

require 添加到此上下文的顶层

# File rdoc/code_object/context.rb, line 568
def add_require(require)
  return require unless @document_self

  if RDoc::TopLevel === self then
    add_to @requires, require
  else
    parent.add_require require
  end
end
add_section(title, comment = nil) 单击以切换源

返回具有 title 的部分,如果该部分不存在,则创建该部分。comment 将附加到该部分的注释中。

titlenil 的部分将返回默认部分。

另请参阅 RDoc::Context::Section

# File rdoc/code_object/context.rb, line 586
def add_section title, comment = nil
  if section = @sections[title] then
    section.add_comment comment if comment
  else
    section = Section.new self, title, comment
    @sections[title] = section
  end

  section
end
add_to(array, thing) 单击以切换源

thing 添加到集合 array

# File rdoc/code_object/context.rb, line 600
def add_to array, thing
  array << thing if @document_self

  thing.parent  = self
  thing.store   = @store if @store
  thing.section = current_section
end
any_content(includes = true) 单击以切换源

是否有任何内容?

这意味着以下任何内容:注释、别名、方法、属性、外部别名、require、常量。

除非 includes == false,否则还会检查 includes 和 extends。

# File rdoc/code_object/context.rb, line 616
def any_content(includes = true)
  @any_content ||= !(
    @comment.empty? &&
    @method_list.empty? &&
    @attributes.empty? &&
    @aliases.empty? &&
    @external_aliases.empty? &&
    @requires.empty? &&
    @constants.empty?
  )
  @any_content || (includes && !(@includes + @extends).empty? )
end
child_name(name) 单击以切换源

为具有 name 的子级创建完整名称

# File rdoc/code_object/context.rb, line 632
def child_name name
  if name =~ /^:+/
    $'  #'
  elsif RDoc::TopLevel === self then
    name
  else
    "#{self.full_name}::#{name}"
  end
end
class_attributes() 单击以切换源

类属性

# File rdoc/code_object/context.rb, line 645
def class_attributes
  @class_attributes ||= attributes.select { |a| a.singleton }
end
class_method_list() 单击以切换源

类方法

# File rdoc/code_object/context.rb, line 652
def class_method_list
  @class_method_list ||= method_list.select { |a| a.singleton }
end
classes() 单击以切换源

此上下文中类的数组

# File rdoc/code_object/context.rb, line 659
def classes
  @classes.values
end
classes_and_modules() 单击以切换源

此命名空间中的所有类和模块

# File rdoc/code_object/context.rb, line 666
def classes_and_modules
  classes + modules
end
classes_hash() 单击以切换源

以类名作为键的类哈希表

# File rdoc/code_object/context.rb, line 673
def classes_hash
  @classes
end
current_section() 单击以切换源

新项将添加到其中的当前文档部分。如果 temporary_section 可用,则将使用它。

# File rdoc/code_object/context.rb, line 681
def current_section
  if section = @temporary_section then
    @temporary_section = nil
  else
    section = @current_section
  end

  section
end
defined_in?(file) 单击以切换源

此内容的一部分是否在 file 中定义?

# File rdoc/code_object/context.rb, line 694
def defined_in?(file)
  @in_files.include?(file)
end
each_attribute() { |attribute| ... } 单击以切换源

属性的迭代器

# File rdoc/code_object/context.rb, line 719
def each_attribute # :yields: attribute
  @attributes.each { |a| yield a }
end
each_classmodule() { |module| ... } 单击以切换源

类和模块的迭代器

# File rdoc/code_object/context.rb, line 726
def each_classmodule(&block) # :yields: module
  classes_and_modules.sort.each(&block)
end
each_constant() { |constant| ... } 单击以切换源

常量的迭代器

# File rdoc/code_object/context.rb, line 733
def each_constant # :yields: constant
  @constants.each {|c| yield c}
end
each_extend() { |extend| ... } 点击切换源代码

扩展模块的迭代器

# File rdoc/code_object/context.rb, line 747
def each_extend # :yields: extend
  @extends.each do |e| yield e end
end
each_include() { |include| ... } 点击切换源代码

包含模块的迭代器

# File rdoc/code_object/context.rb, line 740
def each_include # :yields: include
  @includes.each do |i| yield i end
end
each_method() { |method| ... } 点击切换源代码

方法的迭代器

# File rdoc/code_object/context.rb, line 754
def each_method # :yields: method
  return enum_for __method__ unless block_given?

  @method_list.sort.each { |m| yield m }
end
each_section() { |section, constants, attributes| ... } 点击切换源代码

按标题排序的每个部分内容的迭代器。返回 section,该部分的 constants 和该部分的 attributesconstantsattributes 集合已排序。

要检索部分中的方法,请使用带有可选 section 参数的 methods_by_type

注意:请勿编辑此方法返回的集合

# File rdoc/code_object/context.rb, line 770
def each_section # :yields: section, constants, attributes
  return enum_for __method__ unless block_given?

  constants  = @constants.group_by  do |constant|  constant.section end
  attributes = @attributes.group_by do |attribute| attribute.section end

  constants.default  = []
  attributes.default = []

  sort_sections.each do |section|
    yield section, constants[section].select(&:display?).sort, attributes[section].select(&:display?).sort
  end
end
find_attribute(name, singleton) 点击切换源代码

查找具有单例值 singleton 的属性 name

# File rdoc/code_object/context.rb, line 787
def find_attribute(name, singleton)
  name = $1 if name =~ /^(.*)=$/
  @attributes.find { |a| a.name == name && a.singleton == singleton }
end
find_attribute_named(name) 点击切换源代码

在此上下文中查找具有 name 的属性

# File rdoc/code_object/context.rb, line 795
def find_attribute_named(name)
  case name
  when /\A#/ then
    find_attribute name[1..-1], false
  when /\A::/ then
    find_attribute name[2..-1], true
  else
    @attributes.find { |a| a.name == name }
  end
end
find_class_method_named(name) 点击切换源代码

在此上下文中查找具有 name 的类方法

# File rdoc/code_object/context.rb, line 809
def find_class_method_named(name)
  @method_list.find { |meth| meth.singleton && meth.name == name }
end
find_constant_named(name) 点击切换源代码

在此上下文中查找具有 name 的常量

# File rdoc/code_object/context.rb, line 816
def find_constant_named(name)
  @constants.find do |m|
    m.name == name || m.full_name == name
  end
end
find_enclosing_module_named(name) 点击切换源代码

在更高范围查找模块

# File rdoc/code_object/context.rb, line 825
def find_enclosing_module_named(name)
  parent && parent.find_module_named(name)
end
find_external_alias(name, singleton) 点击切换源代码

查找具有单例值 singleton 的外部别名 name

# File rdoc/code_object/context.rb, line 832
def find_external_alias(name, singleton)
  @external_aliases.find { |m| m.name == name && m.singleton == singleton }
end
find_external_alias_named(name) 点击切换源代码

在此上下文中查找具有 name 的外部别名

# File rdoc/code_object/context.rb, line 839
def find_external_alias_named(name)
  case name
  when /\A#/ then
    find_external_alias name[1..-1], false
  when /\A::/ then
    find_external_alias name[2..-1], true
  else
    @external_aliases.find { |a| a.name == name }
  end
end
find_file_named(name) 点击切换源代码

在此上下文中查找具有 name 的文件

# File rdoc/code_object/context.rb, line 853
def find_file_named name
  @store.find_file_named name
end
find_instance_method_named(name) 点击切换源代码

在此上下文中查找具有 name 的实例方法

# File rdoc/code_object/context.rb, line 860
def find_instance_method_named(name)
  @method_list.find { |meth| !meth.singleton && meth.name == name }
end
find_local_symbol(symbol) 点击切换源代码

在此上下文中查找名为 symbol 的方法、常量、属性、外部别名、模块或文件。

# File rdoc/code_object/context.rb, line 868
def find_local_symbol(symbol)
  find_method_named(symbol) or
  find_constant_named(symbol) or
  find_attribute_named(symbol) or
  find_external_alias_named(symbol) or
  find_module_named(symbol) or
  find_file_named(symbol)
end
find_method(name, singleton) 点击切换源代码

查找具有单例值 singleton 的名为 name 的方法。

# File rdoc/code_object/context.rb, line 880
def find_method(name, singleton)
  @method_list.find { |m|
    if m.singleton
      m.name == name && m.singleton == singleton
    else
      m.name == name && !m.singleton && !singleton
    end
  }
end
find_method_named(name) 点击切换源代码

在此上下文中查找具有 name 的实例方法或模块方法

# File rdoc/code_object/context.rb, line 893
def find_method_named(name)
  case name
  when /\A#/ then
    find_method name[1..-1], false
  when /\A::/ then
    find_method name[2..-1], true
  else
    @method_list.find { |meth| meth.name == name }
  end
end
find_module_named(name) 点击切换源代码

使用 ruby 的作用域规则查找具有 name 的模块

# File rdoc/code_object/context.rb, line 907
def find_module_named(name)
  res = @modules[name] || @classes[name]
  return res if res
  return self if self.name == name
  find_enclosing_module_named name
end
find_symbol(symbol) 点击切换源代码

查找 symbol,首先作为模块,然后作为局部符号。

# File rdoc/code_object/context.rb, line 917
def find_symbol(symbol)
  find_symbol_module(symbol) || find_local_symbol(symbol)
end
find_symbol_module(symbol) 点击切换源代码

查找名为 symbol 的模块。

# File rdoc/code_object/context.rb, line 924
def find_symbol_module(symbol)
  result = nil

  # look for a class or module 'symbol'
  case symbol
  when /^::/ then
    result = @store.find_class_or_module symbol
  when /^(\w+):+(.+)$/
    suffix = $2
    top = $1
    searched = self
    while searched do
      mod = searched.find_module_named(top)
      break unless mod
      result = @store.find_class_or_module "#{mod.full_name}::#{suffix}"
      break if result || searched.is_a?(RDoc::TopLevel)
      searched = searched.parent
    end
  else
    searched = self
    while searched do
      result = searched.find_module_named(symbol)
      break if result || searched.is_a?(RDoc::TopLevel)
      searched = searched.parent
    end
  end

  result
end
full_name() 点击切换源代码

此上下文的完整名称。此方法被子类重写。

# File rdoc/code_object/context.rb, line 957
def full_name
  '(unknown)'
end
fully_documented?() 点击切换源代码

此上下文及其方法和常量是否都包含文档?

(是的,完全文档化并不意味着所有内容都包含。)

# File rdoc/code_object/context.rb, line 966
def fully_documented?
  documented? and
    attributes.all? { |a| a.documented? } and
    method_list.all? { |m| m.documented? } and
    constants.all? { |c| c.documented? }
end
http_url(prefix) 点击切换源代码

此 URL 带有一个 prefix

# File rdoc/code_object/context.rb, line 976
def http_url(prefix)
  path = name_for_path
  path = path.gsub(/<<\s*(\w*)/, 'from-\1') if path =~ /<</
  path = [prefix] + path.split('::')

  File.join(*path.compact) + '.html'
end
initialize_methods_etc() 点击切换源代码

设置方法等的默认值

# File rdoc/code_object/context.rb, line 145
def initialize_methods_etc
  @method_list = []
  @attributes  = []
  @aliases     = []
  @requires    = []
  @includes    = []
  @extends     = []
  @constants   = []
  @external_aliases = []
  @current_line_visibility = nil

  # This Hash maps a method name to a list of unmatched aliases (aliases of
  # a method not yet encountered).
  @unmatched_alias_lists = {}

  @methods_hash   = {}
  @constants_hash = {}

  @params = nil

  @store ||= nil
end
instance_attributes() 点击切换源代码

实例属性

# File rdoc/code_object/context.rb, line 987
def instance_attributes
  @instance_attributes ||= attributes.reject { |a| a.singleton }
end
instance_method_list() 点击切换源代码

实例方法

# File rdoc/code_object/context.rb, line 1003
def instance_method_list
  warn '#instance_method_list is obsoleted, please use #instance_methods'
  @instance_methods ||= method_list.reject { |a| a.singleton }
end
instance_methods() 点击切换源代码

实例方法

# File rdoc/code_object/context.rb, line 994
def instance_methods
  @instance_methods ||= method_list.reject { |a| a.singleton }
end
methods_by_type(section = nil) 点击切换源代码

method_list 按类型('class''instance')和可见性(:public:protected:private)分解为嵌套的哈希。

如果提供了 section,则仅返回该 RDoc::Context::Section 中的方法。

# File rdoc/code_object/context.rb, line 1015
def methods_by_type section = nil
  methods = {}

  TYPES.each do |type|
    visibilities = {}
    RDoc::VISIBILITIES.each do |vis|
      visibilities[vis] = []
    end

    methods[type] = visibilities
  end

  each_method do |method|
    next if section and not method.section == section
    methods[method.type][method.visibility] << method
  end

  methods
end
methods_matching(methods, singleton = false) { |m| ... } 点击切换源代码

返回与 methods 中名称列表匹配的 AnyMethod 和 Attr 条目。

# File rdoc/code_object/context.rb, line 1038
def methods_matching(methods, singleton = false, &block)
  (@method_list + @attributes).each do |m|
    yield m if methods.include?(m.name) and m.singleton == singleton
  end

  each_ancestor do |parent|
    parent.methods_matching(methods, singleton, &block)
  end
end
modules() 点击切换源代码

此上下文中的模块数组

# File rdoc/code_object/context.rb, line 1051
def modules
  @modules.values
end
modules_hash() 点击切换源代码

以模块名称为键的模块哈希

# File rdoc/code_object/context.rb, line 1058
def modules_hash
  @modules
end
name_for_path() 点击切换源代码

用于生成 URL 的名称。默认为 #full_name

# File rdoc/code_object/context.rb, line 1066
def name_for_path
  full_name
end
ongoing_visibility=(visibility) 点击切换源代码

将新方法的可见性更改为 visibility

# File rdoc/code_object/context.rb, line 1073
def ongoing_visibility=(visibility)
  @visibility = visibility
end
record_location(top_level) 点击切换源代码

top_level 记录为 self 所在的文件。

# File rdoc/code_object/context.rb, line 1080
def record_location(top_level)
  @in_files << top_level unless @in_files.include?(top_level)
end
remove_from_documentation?() 点击切换源代码

我们是否应该从文档中删除此上下文?

如果满足以下条件,则答案为是

  • received_nodoctrue

  • any_contentfalse(不包括 include)

  • 所有 includes 都是模块(不是字符串),并且其模块具有 #remove_from_documentation? == true

  • 所有类和模块都具有 #remove_from_documentation? == true

# File rdoc/code_object/context.rb, line 1094
def remove_from_documentation?
  @remove_from_documentation ||=
    @received_nodoc &&
    !any_content(false) &&
    @includes.all? { |i| !i.module.is_a?(String) && i.module.remove_from_documentation? } &&
    classes_and_modules.all? { |cm| cm.remove_from_documentation? }
end
remove_invisible(min_visibility) 点击切换源代码

删除可见性小于 min_visibility 的方法和属性。

# File rdoc/code_object/context.rb, line 1107
def remove_invisible min_visibility
  return if [:private, :nodoc].include? min_visibility
  remove_invisible_in @method_list, min_visibility
  remove_invisible_in @attributes, min_visibility
  remove_invisible_in @constants, min_visibility
end
resolve_aliases(added) 点击切换源代码

当刚刚添加方法或属性时,尝试解析未匹配的别名。

# File rdoc/code_object/context.rb, line 1133
def resolve_aliases added
  # resolve any pending unmatched aliases
  key = added.pretty_name
  unmatched_alias_list = @unmatched_alias_lists[key]
  return unless unmatched_alias_list
  unmatched_alias_list.each do |unmatched_alias|
    added.add_alias unmatched_alias, self
    @external_aliases.delete unmatched_alias
  end
  @unmatched_alias_lists.delete key
end
section_contents() 点击切换源代码

返回此上下文中引用的 RDoc::Context::Section 对象,用于目录。

# File rdoc/code_object/context.rb, line 1149
def section_contents
  used_sections = {}

  each_method do |method|
    next unless method.display?

    used_sections[method.section] = true
  end

  # order found sections
  sections = sort_sections.select do |section|
    used_sections[section]
  end

  # only the default section is used
  return [] if
    sections.length == 1 and not sections.first.title

  sections
end
sections() 点击切换源代码

此上下文中的部分

# File rdoc/code_object/context.rb, line 1173
def sections
  @sections.values
end
set_constant_visibility_for(names, visibility) 点击切换源代码

给定一个常量数组 names,将每个常量的可见性设置为 visibility

# File rdoc/code_object/context.rb, line 1202
def set_constant_visibility_for(names, visibility)
  names.each do |name|
    constant = @constants_hash[name] or next
    constant.visibility = visibility
  end
end
set_current_section(title, comment) 点击切换源代码

将当前部分设置为具有 title 的部分。另请参见 add_section

# File rdoc/code_object/context.rb, line 1184
def set_current_section title, comment
  @current_section = add_section title, comment
end
set_visibility_for(methods, visibility, singleton = false) 点击切换源代码

给定一个方法名称数组 methods,将每个方法的可见性设置为 visibility

# File rdoc/code_object/context.rb, line 1192
def set_visibility_for(methods, visibility, singleton = false)
  methods_matching methods, singleton do |m|
    m.visibility = visibility
  end
end
sort_sections() 点击切换源代码

按字母顺序(默认)或以 TomDoc 方式(none、Public、Internal、Deprecated)对部分进行排序

# File rdoc/code_object/context.rb, line 1213
def sort_sections
  titles = @sections.map { |title, _| title }

  if titles.length > 1 and
     TOMDOC_TITLES_SORT ==
       (titles | TOMDOC_TITLES).sort_by { |title| title.to_s } then
    @sections.values_at(*TOMDOC_TITLES).compact
  else
    @sections.sort_by { |title, _|
      title.to_s
    }.map { |_, section|
      section
    }
  end
end
top_level() 点击切换源代码

返回拥有我们的 TopLevel

# File rdoc/code_object/context.rb, line 1239
def top_level
  return @top_level if defined? @top_level
  @top_level = self
  @top_level = @top_level.parent until RDoc::TopLevel === @top_level
  @top_level
end
upgrade_to_class(mod, class_type, enclosing) 点击切换源代码

enclosing 中将 NormalModule mod 升级为 class_type

# File rdoc/code_object/context.rb, line 1249
def upgrade_to_class mod, class_type, enclosing
  enclosing.modules_hash.delete mod.name

  klass = RDoc::ClassModule.from_module class_type, mod
  klass.store = @store

  # if it was there, then we keep it even if done_documenting
  @store.classes_hash[mod.full_name] = klass
  enclosing.classes_hash[mod.name]   = klass

  klass
end