Class: RDoc::Constant
- Inherits:
-
CodeObject
- Object
- CodeObject
- RDoc::Constant
- Defined in:
- lib/rdoc/code_object/constant.rb
Overview
A constant
Constant Summary collapse
- MARSHAL_VERSION =
:nodoc:
0
Constants included from Text
Text::MARKUP_FORMAT, Text::SPACE_SEPARATED_LETTER_CLASS
Instance Attribute Summary collapse
-
#is_alias_for ⇒ Object
The module or class this constant is an alias for, when one was recorded explicitly (by RDoc::Context#add_module_alias, RDoc::ClassModule#update_aliases, or ri marshal load).
-
#is_alias_for_path ⇒ Object
The constant path on the RHS when the RHS is a bare constant reference (+Foo = Bar+ or Foo = Bar::Baz).
-
#name ⇒ Object
The constant’s name.
-
#value ⇒ Object
The constant’s value.
-
#visibility ⇒ Object
The constant’s visibility.
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
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Constants are ordered by name.
-
#==(other) ⇒ Object
Constants are equal when their #parent and #name is the same.
-
#documented? ⇒ Boolean
A constant is documented if it has a comment, or is an alias for a documented class or module.
-
#full_name ⇒ Object
Full constant name including namespace.
-
#initialize(name, value, comment) ⇒ Constant
constructor
Creates a new constant with
name,valueandcomment. -
#inspect ⇒ Object
:nodoc:.
-
#marshal_dump ⇒ Object
Dumps this Constant for use by ri.
-
#marshal_load(array) ⇒ Object
Loads this Constant from
array. -
#path ⇒ Object
Path to this constant for use with HTML generator output.
-
#pretty_print(q) ⇒ Object
:nodoc:.
-
#resolved_alias_target ⇒ Object
Returns the class/module this constant would alias if #is_alias_for_path was set by the parser and that path resolves to a known class/module, or nil.
-
#search_snippet ⇒ Object
Returns an HTML snippet of the comment for search results.
-
#store=(store) ⇒ Object
Sets the store for this class or module and its contained code objects.
-
#to_s ⇒ Object
:nodoc:.
Methods inherited from CodeObject
#display?, #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, #cvs_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(name, value, comment) ⇒ Constant
Creates a new constant with name, value and comment
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rdoc/code_object/constant.rb', line 40 def initialize(name, value, comment) super() @name = name @value = value @is_alias_for = nil @is_alias_for_path = nil @visibility = :public self.comment = comment end |
Instance Attribute Details
#is_alias_for ⇒ Object
The module or class this constant is an alias for, when one was recorded explicitly (by RDoc::Context#add_module_alias, RDoc::ClassModule#update_aliases, or ri marshal load). Pure accessor; see #resolved_alias_target for the opportunistic lookup path.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rdoc/code_object/constant.rb', line 100 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 |
#is_alias_for_path ⇒ Object
The constant path on the RHS when the RHS is a bare constant reference (+Foo = Bar+ or Foo = Bar::Baz). Captured at parse time so #resolved_alias_target doesn’t have to re-derive it from the textual #value. nil for other RHS shapes.
35 36 37 |
# File 'lib/rdoc/code_object/constant.rb', line 35 def is_alias_for_path @is_alias_for_path end |
#name ⇒ Object
The constant’s name
17 18 19 |
# File 'lib/rdoc/code_object/constant.rb', line 17 def name @name end |
#value ⇒ Object
The constant’s value
22 23 24 |
# File 'lib/rdoc/code_object/constant.rb', line 22 def value @value end |
#visibility ⇒ Object
The constant’s visibility
27 28 29 |
# File 'lib/rdoc/code_object/constant.rb', line 27 def visibility @visibility end |
Instance Method Details
#<=>(other) ⇒ Object
Constants are ordered by name
56 57 58 59 60 |
# File 'lib/rdoc/code_object/constant.rb', line 56 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
65 66 67 68 69 |
# File 'lib/rdoc/code_object/constant.rb', line 65 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.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rdoc/code_object/constant.rb', line 75 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_name ⇒ Object
Full constant name including namespace
90 91 92 |
# File 'lib/rdoc/code_object/constant.rb', line 90 def full_name @full_name ||= "#{parent_name}::#{@name}" end |
#inspect ⇒ Object
:nodoc:
125 126 127 128 129 130 |
# File 'lib/rdoc/code_object/constant.rb', line 125 def inspect # :nodoc: "#<%s:0x%x %s::%s>" % [ self.class, object_id, parent_name, @name, ] end |
#marshal_dump ⇒ Object
Dumps this Constant for use by ri. See also #marshal_load
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rdoc/code_object/constant.rb', line 135 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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rdoc/code_object/constant.rb', line 161 def marshal_load(array) initialize array[1], nil, RDoc::Comment.from_document(array[5]) @full_name = array[2] @visibility = array[3] || :public @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 |
#path ⇒ Object
Path to this constant for use with HTML generator output.
179 180 181 |
# File 'lib/rdoc/code_object/constant.rb', line 179 def path "#{@parent.path}##{@name}" end |
#pretty_print(q) ⇒ Object
:nodoc:
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/rdoc/code_object/constant.rb', line 192 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 |
#resolved_alias_target ⇒ Object
Returns the class/module this constant would alias if #is_alias_for_path was set by the parser and that path resolves to a known class/module, or nil. Used to support ‘Const = RHS` parsed before `class RHS;end` is defined in another file. Pure lookup; does not mutate state. Honors :nodoc: (returns nil if document_self is false). Note that module nesting information is lost, so constant lookup is inaccurate.
119 120 121 122 123 |
# File 'lib/rdoc/code_object/constant.rb', line 119 def resolved_alias_target return nil unless document_self return nil unless @is_alias_for_path parent.find_module_named(@is_alias_for_path) end |
#search_snippet ⇒ Object
Returns an HTML snippet of the comment for search results.
186 187 188 189 190 |
# File 'lib/rdoc/code_object/constant.rb', line 186 def search_snippet return '' if comment.empty? snippet(comment) end |
#store=(store) ⇒ Object
Sets the store for this class or module and its contained code objects.
206 207 208 209 210 |
# File 'lib/rdoc/code_object/constant.rb', line 206 def store=(store) super @file = @store.add_file @file.full_name if @file end |
#to_s ⇒ Object
:nodoc:
212 213 214 215 216 217 218 219 |
# File 'lib/rdoc/code_object/constant.rb', line 212 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 |