Class: RDoc::TopLevel

Inherits:
Context show all
Defined in:
lib/rdoc/code_object/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::SPACE_SEPARATED_LETTER_CLASS

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

Attributes inherited from CodeObject

#comment, #document_children, #document_self, #done_documenting, #file, #force_documentation, #line, #metadata, #mixin_from, #parent, #received_nodoc, #section, #store

Attributes included from Text

#language

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_module_by_normal_module, #add_require, #add_section, #add_to, #any_content, #child_name, #class_method_list, #classes, #classes_and_modules, #classes_hash, #display, #each_ancestor, #each_classmodule, #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_instance_method_named, #find_method, #find_method_named, #find_or_create_constant_owner_for_path, #find_or_create_namespace_path, #find_symbol, #find_symbol_module, #fully_documented?, #initialize_methods_etc, #instance_methods, #methods_by_type, #methods_matching, #modules, #modules_hash, #name_for_path, #record_location, #remove_from_documentation?, #remove_invisible, #remove_invisible_in, #resolve_aliases, #section_contents, #sections, #sections_hash, #set_constant_visibility_for, #set_current_section, #set_visibility_for, #sort_sections, #top_level, #upgrade_to_class

Methods inherited from CodeObject

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

Methods included from Generator::Markup

#aref_to, #as_href, #canonical_url, #description, #formatter

Methods included from Text

decode_legacy_label, expand_tabs, #flush_left, #markup, #normalize_comment, #parse, #snippet, #strip_hashes, #strip_newlines, #strip_stars, to_anchor, #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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

Instance Attribute Details

#absolute_name ⇒ Object

Absolute name of this file



18
19
20
# File 'lib/rdoc/code_object/top_level.rb', line 18

def absolute_name
  @absolute_name
end

#base_name ⇒ Object (readonly) Also known as: name

Base name of this file



23
24
25
# File 'lib/rdoc/code_object/top_level.rb', line 23

def base_name
  @base_name
end

#classes_or_modules ⇒ Object (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.



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

def classes_or_modules
  @classes_or_modules
end

#page_name ⇒ Object (readonly)

Base name of this file without the extension



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

def page_name
  @page_name
end

#parser ⇒ Object

The parser class that processed this file



40
41
42
# File 'lib/rdoc/code_object/top_level.rb', line 40

def parser
  @parser
end

#relative_name ⇒ Object

Relative name of this file



13
14
15
# File 'lib/rdoc/code_object/top_level.rb', line 13

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



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

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.



77
78
79
80
81
# 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

#add_constant(constant) ⇒ Object

Adds constant to Object instead of self.



86
87
88
89
90
# 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

#add_include(include) ⇒ Object

Adds include to Object instead of self.



95
96
97
98
99
# 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

#add_method(method) ⇒ Object

Adds method to Object instead of self.



104
105
106
107
108
# 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

#add_to_classes_or_modules(mod) ⇒ Object

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



114
115
116
# File 'lib/rdoc/code_object/top_level.rb', line 114

def add_to_classes_or_modules(mod)
  @classes_or_modules << mod
end

#cvs_url ⇒ Object

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



203
204
205
206
207
208
209
210
211
# 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

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



126
127
128
# File 'lib/rdoc/code_object/top_level.rb', line 126

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



133
134
135
# File 'lib/rdoc/code_object/top_level.rb', line 133

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

#find_module_named(name) ⇒ Object Also known as: get_module_named

Finds a module or class with name



140
141
142
# File 'lib/rdoc/code_object/top_level.rb', line 140

def find_module_named(name)
  find_class_or_module(name)
end

#full_name ⇒ Object

Returns the relative name of this file



149
150
151
# File 'lib/rdoc/code_object/top_level.rb', line 149

def full_name
  @relative_name
end

#hash ⇒ Object

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



157
158
159
# File 'lib/rdoc/code_object/top_level.rb', line 157

def hash
  @relative_name.hash
end

#http_url ⇒ Object

URL for this with a prefix



164
165
166
# File 'lib/rdoc/code_object/top_level.rb', line 164

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

#inspect ⇒ Object

:nodoc:



168
169
170
171
172
173
174
175
# File 'lib/rdoc/code_object/top_level.rb', line 168

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

#marshal_dump ⇒ Object

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



180
181
182
183
184
185
186
187
# File 'lib/rdoc/code_object/top_level.rb', line 180

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

#marshal_load(array) ⇒ Object

Loads this TopLevel from array.



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

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

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

#object_class ⇒ Object

Returns the NormalClass "Object", creating it if not found.

Records self as a location in "Object".



204
205
206
207
208
209
210
# 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 ⇒ Object

Path to this file for use with HTML generator output.



215
216
217
218
219
220
221
222
223
224
225
# 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

#pretty_print(q) ⇒ Object

:nodoc:



227
228
229
230
231
232
233
234
235
236
# File 'lib/rdoc/code_object/top_level.rb', line 227

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 ⇒ Object

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.



244
245
246
247
248
249
250
251
252
253
254
255
256
# 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

#search_snippet ⇒ Object

Returns an HTML snippet of the comment for search results.



261
262
263
264
265
# File 'lib/rdoc/code_object/top_level.rb', line 261

def search_snippet
  return '' if @comment.empty?

  snippet(@comment)
end

#text? ⇒ Boolean

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

Returns:

  • (Boolean)


270
271
272
# File 'lib/rdoc/code_object/top_level.rb', line 270

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

#to_s ⇒ Object

:nodoc:



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

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