Class: RDoc::TopLevel

Inherits:
Context show all
Defined in:
lib/rdoc/top_level.rb,
lib/rdoc/generator/markup.rb

Overview

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

Constant Summary collapse

MARSHAL_VERSION =

:nodoc:

0

Constants inherited from Context

Context::TOMDOC_TITLES, Context::TOMDOC_TITLES_SORT, Context::TYPES

Constants included from Text

RDoc::Text::MARKUP_FORMAT, RDoc::Text::TO_HTML_CHARACTERS

Instance Attribute Summary collapse

Attributes inherited from Context

#aliases, #attributes, #block_params, #constants, #constants_hash, #current_section, #extends, #external_aliases, #in_files, #includes, #method_list, #methods_hash, #params, #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, #store, #viewer

Instance Method Summary collapse

Methods inherited from Context

#<=>, #add, #add_attribute, #add_class, #add_class_or_module, #add_extend, #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_ancestor, #each_attribute, #each_classmodule, #each_constant, #each_extend, #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_method, #find_method_named, #find_symbol, #find_symbol_module, #fully_documented?, #initialize_methods_etc, #instance_attributes, #instance_method_list, #methods_by_type, #methods_matching, #modules, #modules_hash, #name_for_path, #ongoing_visibility=, #record_location, #remove_from_documentation?, #remove_invisible, #remove_invisible_in, #resolve_aliases, #section_contents, #sections, #sections_hash, #set_current_section, #set_visibility_for, #sort_sections, #top_level, #upgrade_to_class

Methods inherited from CodeObject

#documented?, #each_parent, #file_name, #full_name=, #ignore, #ignored?, #initialize_visibility, #options, #parent_file_name, #parent_name, #record_location, #start_doc, #stop_doc, #suppress, #suppressed?

Methods included from Generator::Markup

#aref_to, #as_href, #description, #formatter

Methods included from Text

encode_fallback, #expand_tabs, #flush_left, #markup, #normalize_comment, #parse, #snippet, #strip_hashes, #strip_newlines, #strip_stars, #to_html, #wrap

Constructor Details

#initialize(absolute_name, relative_name = absolute_name) ⇒ TopLevel

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.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rdoc/top_level.rb', line 42

def initialize absolute_name, relative_name = absolute_name
  super()
  @name = nil
  @absolute_name = absolute_name
  @relative_name = relative_name
  @file_stat     = File.stat(absolute_name) rescue nil # HACK for testing
  @diagram       = nil
  @parser        = nil

  @classes_or_modules = []
end

Instance Attribute Details

#absolute_nameObject

Absolute name of this file



21
22
23
# File 'lib/rdoc/top_level.rb', line 21

def absolute_name
  @absolute_name
end

#classes_or_modulesObject (readonly)

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.



28
29
30
# File 'lib/rdoc/top_level.rb', line 28

def classes_or_modules
  @classes_or_modules
end

#diagramObject

:nodoc:



30
31
32
# File 'lib/rdoc/top_level.rb', line 30

def diagram
  @diagram
end

#file_statObject

This TopLevel’s File::Stat struct



11
12
13
# File 'lib/rdoc/top_level.rb', line 11

def file_stat
  @file_stat
end

#parserObject

The parser class that processed this file



35
36
37
# File 'lib/rdoc/top_level.rb', line 35

def parser
  @parser
end

#relative_nameObject

Relative name of this file



16
17
18
# File 'lib/rdoc/top_level.rb', line 16

def relative_name
  @relative_name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

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



57
58
59
# File 'lib/rdoc/top_level.rb', line 57

def == other
  self.class === other and @relative_name == other.relative_name
end

#add_alias(an_alias) ⇒ Object

Adds an_alias to Object instead of self.



66
67
68
69
70
# File 'lib/rdoc/top_level.rb', line 66

def add_alias(an_alias)
  object_class.record_location self
  return an_alias unless @document_self
  object_class.add_alias an_alias
end

#add_constant(constant) ⇒ Object

Adds constant to Object instead of self.



75
76
77
78
79
# File 'lib/rdoc/top_level.rb', line 75

def add_constant constant
  object_class.record_location self
  return constant unless @document_self
  object_class.add_constant constant
end

#add_include(include) ⇒ Object

Adds include to Object instead of self.



84
85
86
87
88
# File 'lib/rdoc/top_level.rb', line 84

def add_include(include)
  object_class.record_location self
  return include unless @document_self
  object_class.add_include include
end

#add_method(method) ⇒ Object

Adds method to Object instead of self.



93
94
95
96
97
# File 'lib/rdoc/top_level.rb', line 93

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

#add_to_classes_or_modules(mod) ⇒ Object

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



103
104
105
# File 'lib/rdoc/top_level.rb', line 103

def add_to_classes_or_modules mod
  @classes_or_modules << mod
end

#base_nameObject Also known as: name

Base name of this file



110
111
112
# File 'lib/rdoc/top_level.rb', line 110

def base_name
  File.basename @relative_name
end

#cvs_urlObject

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



158
159
160
161
162
163
164
165
166
# File 'lib/rdoc/generator/markup.rb', line 158

def cvs_url
  url = @store.rdoc.options.webcvs

  if /%s/ =~ url then
    url % @relative_name
  else
    url + @relative_name
  end
end

#display?Boolean

Only a TopLevel that contains text file) will be displayed. See also RDoc::CodeObject#display?

Returns:

  • (Boolean)


120
121
122
# File 'lib/rdoc/top_level.rb', line 120

def display?
  text? and super
end

#find_class_or_module(name) ⇒ Object

See RDoc::TopLevel::find_class_or_module – TODO Why do we search through all classes/modules found, not just the

ones of this instance?


130
131
132
# File 'lib/rdoc/top_level.rb', line 130

def find_class_or_module name
  @store.find_class_or_module name
end

#find_local_symbol(symbol) ⇒ Object

Finds a class or module named symbol



137
138
139
# File 'lib/rdoc/top_level.rb', line 137

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

#find_module_named(name) ⇒ Object

Finds a module or class with name



144
145
146
# File 'lib/rdoc/top_level.rb', line 144

def find_module_named(name)
  find_class_or_module(name)
end

#full_nameObject

Returns the relative name of this file



151
152
153
# File 'lib/rdoc/top_level.rb', line 151

def full_name
  @relative_name
end

#hashObject

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



159
160
161
# File 'lib/rdoc/top_level.rb', line 159

def hash
  @relative_name.hash
end

#http_url(prefix) ⇒ Object

URL for this with a prefix



166
167
168
169
170
# File 'lib/rdoc/top_level.rb', line 166

def http_url(prefix)
  path = [prefix, @relative_name.tr('.', '_')]

  File.join(*path.compact) + '.html'
end

#inspectObject

:nodoc:



172
173
174
175
176
177
178
179
# File 'lib/rdoc/top_level.rb', line 172

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

#last_modifiedObject

Time this file was last modified, if known



184
185
186
# File 'lib/rdoc/top_level.rb', line 184

def last_modified
  @file_stat ? file_stat.mtime : nil
end

#marshal_dumpObject

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



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

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

#marshal_load(array) ⇒ Object

Loads this TopLevel from array.



203
204
205
206
207
208
209
210
# File 'lib/rdoc/top_level.rb', line 203

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

  @parser  = array[2]
  @comment = array[3]

  @file_stat          = nil
end

#object_classObject

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

Records self as a location in “Object”.



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

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

#page_nameObject

Base name of this file without the extension



228
229
230
231
232
233
# File 'lib/rdoc/top_level.rb', line 228

def page_name
  basename = File.basename @relative_name
  basename =~ /\.(rb|rdoc|txt|md)$/i

  $` || basename
end

#pathObject

Path to this file for use with HTML generator output.



238
239
240
# File 'lib/rdoc/top_level.rb', line 238

def path
  http_url @store.rdoc.generator.file_dir
end

#pretty_print(q) ⇒ Object

:nodoc:



242
243
244
245
246
247
248
249
250
251
# File 'lib/rdoc/top_level.rb', line 242

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_recordObject

Search record used by RDoc::Generator::JsonIndex



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rdoc/top_level.rb', line 256

def search_record
  return unless @parser < RDoc::Parser::Text

  [
    page_name,
    '',
    page_name,
    '',
    path,
    '',
    snippet(@comment),
  ]
end

#text?Boolean

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

Returns:

  • (Boolean)


273
274
275
# File 'lib/rdoc/top_level.rb', line 273

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

#to_sObject

:nodoc:



277
278
279
# File 'lib/rdoc/top_level.rb', line 277

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