class RDoc::TopLevel

A TopLevel context is a representation of the contents of a single file

Attributes

Absolute name of this file

Base name of this file

All the classes or modules that were declared in this file. These are assigned to either classes_hash or modules_hash once we know what they really are.

Base name of this file without the extension

The parser class that processed this file

Relative name of this file

Public Class Methods

Creates a new TopLevel for the file at absolute_name. If documentation is being generated outside the source dir relative_name is relative to the source directory.

Calls superclass method RDoc::Context::new
# File lib/rdoc/code_object/top_level.rb, line 47
def initialize(absolute_name, relative_name = absolute_name)
  super()
  @name = nil
  @absolute_name = absolute_name
  @relative_name = relative_name
  @parser        = nil

  if relative_name
    @base_name = File.basename(relative_name)
    @page_name = @base_name.sub(/\.(rb|rdoc|txt|md)\z/i, '')
  else
    @base_name = nil
    @page_name = nil
  end

  @classes_or_modules = []
end

Public Instance Methods

An RDoc::TopLevel is equal to another with the same relative_name

# File lib/rdoc/code_object/top_level.rb, line 68
def ==(other)
  self.class === other and @relative_name == other.relative_name
end
Also aliased as: eql?

Adds an_alias to Object instead of self.

# File lib/rdoc/code_object/top_level.rb, line 77
def add_alias(an_alias)
  object_class.record_location self
  return an_alias unless @document_self
  object_class.add_alias an_alias
end

Adds constant to Object instead of self.

# File lib/rdoc/code_object/top_level.rb, line 86
def add_constant(constant)
  object_class.record_location self
  return constant unless @document_self
  object_class.add_constant constant
end

Adds include to Object instead of self.

# File lib/rdoc/code_object/top_level.rb, line 95
  def add_include(include)
    object_class.record_location self
    return include unless @document_self
    object_class.add_include include
  end

  ##
  # Adds +method+ to +Object+ instead of +self+.

  def add_method(method)
    object_class.record_location self
    return method unless @document_self
    object_class.add_method method
  end

  ##
  # Adds class or module +mod+. Used in the building phase
  # by the Ruby parser.

  def add_to_classes_or_modules(mod)
    @classes_or_modules << mod
  end

  alias name base_name

  ##
  # See RDoc::TopLevel::find_class_or_module
  #--
  # TODO Why do we search through all classes/modules found, not just the
  #       ones of this instance?

  def find_class_or_module(name)
    @store.find_class_or_module name
  end

  ##
  # Finds a class or module named +symbol+

  def find_local_symbol(symbol)
    find_class_or_module(symbol) || super
  end

  ##
  # Finds a module or class with +name+

  def find_module_named(name)
    find_class_or_module(name)
  end

  alias get_module_named find_module_named

  ##
  # Returns the relative name of this file

  def full_name
    @relative_name
  end

  ##
  # An RDoc::TopLevel has the same hash as another with the same
  # relative_name

  def hash
    @relative_name.hash
  end

  ##
  # URL for this with a +prefix+

  def http_url
    @relative_name.tr('.', '_') + '.html'
  end

  def inspect # :nodoc:
    "#<%s:0x%x %p modules: %p classes: %p>" % [
      self.class, object_id,
      base_name,
      @modules.map { |n, m| m },
      @classes.map { |n, c| c }
    ]
  end

  ##
  # Dumps this TopLevel for use by ri.  See also #marshal_load

  def marshal_dump
    [
      MARSHAL_VERSION,
      @relative_name,
      @parser,
      parse(@comment),
    ]
  end

  ##
  # Loads this TopLevel from +array+.

  def marshal_load(array) # :nodoc:
    initialize array[1]

    @parser  = array[2]
    @comment = Comment.from_document array[3]
  end

  ##
  # Returns the NormalClass "Object", creating it if not found.
  #
  # Records +self+ as a location in "Object".

  def object_class
    @object_class ||= begin
      oc = @store.find_class_named('Object') || add_class(NormalClass, 'Object')
      oc.record_location self
      oc
    end
  end

  ##
  # Path to this file for use with HTML generator output.

  def path
    base = if options.main_page == full_name
             'index.html'
           else
             http_url
           end

    prefix = options.file_path_prefix
    return base unless prefix
    File.join(prefix, base)
  end

  def pretty_print(q) # :nodoc:
    q.group 2, "[#{self.class}: ", "]" do
      q.text "base name: #{base_name.inspect}"
      q.breakable

      items = @modules.map { |n, m| m }
      items.concat @modules.map { |n, c| c }
      q.seplist items do |mod| q.pp mod end
    end
  end

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

  def search_record
    return unless @parser < Parser::Text

    [
      page_name,
      '',
      page_name,
      '',
      path,
      '',
      search_snippet,
    ]
  end

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

  def search_snippet
    return '' if @comment.empty?

    snippet(@comment)
  end

  ##
  # Is this TopLevel from a text file instead of a source code file?

  def text?
    @parser and @parser.include? Parser::Text
  end

  def to_s # :nodoc:
    "file #{full_name}"
  end

end

Adds method to Object instead of self.

# File lib/rdoc/code_object/top_level.rb, line 104
def add_method(method)
  object_class.record_location self
  return method unless @document_self
  object_class.add_method method
end

Adds class or module mod. Used in the building phase by the Ruby parser.

# File lib/rdoc/code_object/top_level.rb, line 114
def add_to_classes_or_modules(mod)
  @classes_or_modules << mod
end

Returns a URL for this source file on some web repository. Use the -W command line option to set.

# File lib/rdoc/generator/markup.rb, line 203
def cvs_url
  url = @store.options.webcvs

  if /%s/ =~ url
    url % @relative_name
  else
    url + @relative_name
  end
end
Alias for: ==

See RDoc::TopLevel::find_class_or_module

# File lib/rdoc/code_object/top_level.rb, line 126
def find_class_or_module(name)
  @store.find_class_or_module name
end

Finds a class or module named symbol

Calls superclass method RDoc::Context#find_local_symbol
# File lib/rdoc/code_object/top_level.rb, line 133
def find_local_symbol(symbol)
  find_class_or_module(symbol) || super
end

Finds a module or class with name

# File lib/rdoc/code_object/top_level.rb, line 140
def find_module_named(name)
  find_class_or_module(name)
end

Returns the relative name of this file

# File lib/rdoc/code_object/top_level.rb, line 149
def full_name
  @relative_name
end

An RDoc::TopLevel has the same hash as another with the same relative_name

# File lib/rdoc/code_object/top_level.rb, line 157
def hash
  @relative_name.hash
end

URL for this with a prefix

# File lib/rdoc/code_object/top_level.rb, line 164
def http_url
  @relative_name.tr('.', '_') + '.html'
end

Dumps this TopLevel for use by ri. See also marshal_load

# File lib/rdoc/code_object/top_level.rb, line 180
def marshal_dump
  [
    MARSHAL_VERSION,
    @relative_name,
    @parser,
    parse(@comment),
  ]
end

Returns the NormalClass “Object”, creating it if not found.

Records self as a location in “Object”.

# File lib/rdoc/code_object/top_level.rb, line 204
def object_class
  @object_class ||= begin
    oc = @store.find_class_named('Object') || add_class(NormalClass, 'Object')
    oc.record_location self
    oc
  end
end

Path to this file for use with HTML generator output.

# File lib/rdoc/code_object/top_level.rb, line 215
def path
  base = if options.main_page == full_name
           'index.html'
         else
           http_url
         end

  prefix = options.file_path_prefix
  return base unless prefix
  File.join(prefix, base)
end

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.

# File lib/rdoc/code_object/top_level.rb, line 244
def search_record
  return unless @parser < Parser::Text

  [
    page_name,
    '',
    page_name,
    '',
    path,
    '',
    search_snippet,
  ]
end

Returns an HTML snippet of the comment for search results.

# File lib/rdoc/code_object/top_level.rb, line 261
def search_snippet
  return '' if @comment.empty?

  snippet(@comment)
end

Is this TopLevel from a text file instead of a source code file?

# File lib/rdoc/code_object/top_level.rb, line 270
def text?
  @parser and @parser.include? Parser::Text
end