Class: RDoc::Constant

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

Overview

A constant

Constant Summary collapse

MARSHAL_VERSION =

:nodoc:

0

Constants included from Text

Text::MARKUP_FORMAT, 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, #store, #viewer

Instance Method Summary collapse

Methods inherited from CodeObject

#display?, #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, #cvs_url, #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(name, value, comment) ⇒ Constant

Creates a new constant with name, value and comment



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rdoc/constant.rb', line 31

def initialize(name, value, comment)
  super()

  @name  = name
  @value = value

  @is_alias_for = nil
  @visibility   = nil

  self.comment = comment
end

Instance Attribute Details

#is_alias_forObject

The module or class this constant is an alias for



87
88
89
90
91
92
93
94
95
96
# File 'lib/rdoc/constant.rb', line 87

def is_alias_for
  case @is_alias_for
  when String then
    found = @store.find_class_or_module @is_alias_for
    @is_alias_for = found if found
    @is_alias_for
  else
    @is_alias_for
  end
end

#nameObject

The constant’s name



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

def name
  @name
end

#valueObject

The constant’s value



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

def value
  @value
end

#visibilityObject

The constant’s visibility



26
27
28
# File 'lib/rdoc/constant.rb', line 26

def visibility
  @visibility
end

Instance Method Details

#<=>(other) ⇒ Object

Constants are ordered by name



46
47
48
49
50
# File 'lib/rdoc/constant.rb', line 46

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

  [parent_name, name] <=> [other.parent_name, other.name]
end

#==(other) ⇒ Object

Constants are equal when their #parent and #name is the same



55
56
57
58
59
# File 'lib/rdoc/constant.rb', line 55

def == other
  self.class == other.class and
    @parent == other.parent and
    @name == other.name
end

#documented?Boolean

A constant is documented if it has a comment, or is an alias for a documented class or module.

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rdoc/constant.rb', line 65

def documented?
  return true if super
  return false unless @is_alias_for
  case @is_alias_for
  when String then
    found = @store.find_class_or_module @is_alias_for
    return false unless found
    @is_alias_for = found
  end
  @is_alias_for.documented?
end

#full_nameObject

Full constant name including namespace



80
81
82
# File 'lib/rdoc/constant.rb', line 80

def full_name
  @full_name ||= "#{parent_name}::#{@name}"
end

#inspectObject

:nodoc:



98
99
100
101
102
103
# File 'lib/rdoc/constant.rb', line 98

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

#marshal_dumpObject

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rdoc/constant.rb', line 108

def marshal_dump
  alias_name = case found = is_alias_for
               when RDoc::CodeObject then found.full_name
               else                       found
               end

  [ MARSHAL_VERSION,
    @name,
    full_name,
    @visibility,
    alias_name,
    parse(@comment),
    @file.relative_name,
    parent.name,
    parent.class,
    section.title,
  ]
end

#marshal_load(array) ⇒ Object

Loads this Constant from array. For a loaded Constant the following methods will return cached values:

  • #full_name

  • #parent_name



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rdoc/constant.rb', line 134

def marshal_load array
  initialize array[1], nil, array[5]

  @full_name     = array[2]
  @visibility    = array[3]
  @is_alias_for  = array[4]
  #                      5 handled above
  #                      6 handled below
  @parent_name   = array[7]
  @parent_class  = array[8]
  @section_title = array[9]

  @file = RDoc::TopLevel.new array[6]
end

#pathObject

Path to this constant for use with HTML generator output.



152
153
154
# File 'lib/rdoc/constant.rb', line 152

def path
  "#{@parent.path}##{@name}"
end

#pretty_print(q) ⇒ Object

:nodoc:



156
157
158
159
160
161
162
163
164
165
# File 'lib/rdoc/constant.rb', line 156

def pretty_print q # :nodoc:
  q.group 2, "[#{self.class.name} #{full_name}", "]" do
    unless comment.empty? then
      q.breakable
      q.text "comment:"
      q.breakable
      q.pp @comment
    end
  end
end

#store=(store) ⇒ Object

Sets the store for this class or module and its contained code objects.



170
171
172
173
174
# File 'lib/rdoc/constant.rb', line 170

def store= store
  super

  @file = @store.add_file @file.full_name if @file
end

#to_sObject

:nodoc:



176
177
178
179
180
181
182
183
# File 'lib/rdoc/constant.rb', line 176

def to_s # :nodoc:
  parent_name = parent ? parent.full_name : '(unknown)'
  if is_alias_for
    "constant #{parent_name}::#@name -> #{is_alias_for}"
  else
    "constant #{parent_name}::#@name"
  end
end