Class: RDoc::Include

Inherits:
CodeObject show all
Defined in:
lib/rdoc/include.rb

Overview

A Module include in a class with #include

Constant Summary

Constants included from Text

Text::TO_HTML_CHARACTERS

Instance Attribute Summary collapse

Attributes inherited from CodeObject

#comment, #document_children, #document_self, #done_documenting, #file, #force_documentation, #line, #metadata, #offset, #parent, #received_nodoc, #section, #viewer

Instance Method Summary collapse

Methods inherited from CodeObject

#display?, #documented?, #each_parent, #file_name, #full_name=, #ignore, #ignored?, #parent_file_name, #parent_name, #record_location, #start_doc, #stop_doc

Methods included from Text

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

Constructor Details

#initialize(name, comment) ⇒ Include

Creates a new Include for name with comment



16
17
18
19
20
21
# File 'lib/rdoc/include.rb', line 16

def initialize(name, comment)
  super()
  @name = name
  self.comment = comment
  @module = nil   # cache for module if found
end

Instance Attribute Details

#nameObject

Name of included module



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

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object

Includes are sorted by name



26
27
28
29
30
# File 'lib/rdoc/include.rb', line 26

def <=> other
  return unless self.class === other

  name <=> other.name
end

#==(other) ⇒ Object

:nodoc:



32
33
34
35
# File 'lib/rdoc/include.rb', line 32

def == other # :nodoc:
  self.class == other.class and
    self.name == other.name
end

#full_nameObject

Full name based on #module



40
41
42
43
# File 'lib/rdoc/include.rb', line 40

def full_name
  m = self.module
  RDoc::ClassModule === m ? m.full_name : @name
end

#inspectObject

:nodoc:



45
46
47
48
49
50
51
# File 'lib/rdoc/include.rb', line 45

def inspect # :nodoc:
  "#<%s:0x%x %s.include %s>" % [
    self.class,
    object_id,
    parent_name, @name,
  ]
end

#moduleObject

Attempts to locate the included module object. Returns the name if not known.

The scoping rules of Ruby to resolve the name of an included module are:

  • first look into the children of the current context;

  • if not found, look into the children of included modules, in reverse inclusion order;

  • if still not found, go up the hierarchy of names.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rdoc/include.rb', line 63

def module
  return @module if @module

  # search the current context
  return @name unless parent
  full_name = parent.child_name(@name)
  @module = RDoc::TopLevel.modules_hash[full_name]
  return @module if @module
  return @name if @name =~ /^::/

  # search the includes before this one, in reverse order
  searched = parent.includes.take_while { |i| i != self }.reverse
  searched.each do |i|
    inc = i.module
    next if String === inc
    full_name = inc.child_name(@name)
    @module = RDoc::TopLevel.modules_hash[full_name]
    return @module if @module
  end

  # go up the hierarchy of names
  p = parent.parent
  while p
    full_name = p.child_name(@name)
    @module = RDoc::TopLevel.modules_hash[full_name]
    return @module if @module
    p = p.parent
  end

  @name
end

#to_sObject

:nodoc:



95
96
97
# File 'lib/rdoc/include.rb', line 95

def to_s # :nodoc:
  "include #@name in: #{parent}"
end