/vendor/bundle/gems/rdoc-4.2.2/lib/rdoc/top_level.rb

https://gitlab.com/Renatafsouza/EP3OO · Ruby · 282 lines · 139 code · 73 blank · 70 comment · 5 complexity · 183057628dc116b3a727f1f5dbc4ec8b MD5 · raw file

  1. ##
  2. # A TopLevel context is a representation of the contents of a single file
  3. class RDoc::TopLevel < RDoc::Context
  4. MARSHAL_VERSION = 0 # :nodoc:
  5. ##
  6. # This TopLevel's File::Stat struct
  7. attr_accessor :file_stat
  8. ##
  9. # Relative name of this file
  10. attr_accessor :relative_name
  11. ##
  12. # Absolute name of this file
  13. attr_accessor :absolute_name
  14. ##
  15. # All the classes or modules that were declared in
  16. # this file. These are assigned to either +#classes_hash+
  17. # or +#modules_hash+ once we know what they really are.
  18. attr_reader :classes_or_modules
  19. attr_accessor :diagram # :nodoc:
  20. ##
  21. # The parser class that processed this file
  22. attr_accessor :parser
  23. ##
  24. # Creates a new TopLevel for the file at +absolute_name+. If documentation
  25. # is being generated outside the source dir +relative_name+ is relative to
  26. # the source directory.
  27. def initialize absolute_name, relative_name = absolute_name
  28. super()
  29. @name = nil
  30. @absolute_name = absolute_name
  31. @relative_name = relative_name
  32. @file_stat = File.stat(absolute_name) rescue nil # HACK for testing
  33. @diagram = nil
  34. @parser = nil
  35. @classes_or_modules = []
  36. end
  37. ##
  38. # An RDoc::TopLevel is equal to another with the same relative_name
  39. def == other
  40. self.class === other and @relative_name == other.relative_name
  41. end
  42. alias eql? ==
  43. ##
  44. # Adds +an_alias+ to +Object+ instead of +self+.
  45. def add_alias(an_alias)
  46. object_class.record_location self
  47. return an_alias unless @document_self
  48. object_class.add_alias an_alias
  49. end
  50. ##
  51. # Adds +constant+ to +Object+ instead of +self+.
  52. def add_constant constant
  53. object_class.record_location self
  54. return constant unless @document_self
  55. object_class.add_constant constant
  56. end
  57. ##
  58. # Adds +include+ to +Object+ instead of +self+.
  59. def add_include(include)
  60. object_class.record_location self
  61. return include unless @document_self
  62. object_class.add_include include
  63. end
  64. ##
  65. # Adds +method+ to +Object+ instead of +self+.
  66. def add_method(method)
  67. object_class.record_location self
  68. return method unless @document_self
  69. object_class.add_method method
  70. end
  71. ##
  72. # Adds class or module +mod+. Used in the building phase
  73. # by the Ruby parser.
  74. def add_to_classes_or_modules mod
  75. @classes_or_modules << mod
  76. end
  77. ##
  78. # Base name of this file
  79. def base_name
  80. File.basename @relative_name
  81. end
  82. alias name base_name
  83. ##
  84. # Only a TopLevel that contains text file) will be displayed. See also
  85. # RDoc::CodeObject#display?
  86. def display?
  87. text? and super
  88. end
  89. ##
  90. # See RDoc::TopLevel::find_class_or_module
  91. #--
  92. # TODO Why do we search through all classes/modules found, not just the
  93. # ones of this instance?
  94. def find_class_or_module name
  95. @store.find_class_or_module name
  96. end
  97. ##
  98. # Finds a class or module named +symbol+
  99. def find_local_symbol(symbol)
  100. find_class_or_module(symbol) || super
  101. end
  102. ##
  103. # Finds a module or class with +name+
  104. def find_module_named(name)
  105. find_class_or_module(name)
  106. end
  107. ##
  108. # Returns the relative name of this file
  109. def full_name
  110. @relative_name
  111. end
  112. ##
  113. # An RDoc::TopLevel has the same hash as another with the same
  114. # relative_name
  115. def hash
  116. @relative_name.hash
  117. end
  118. ##
  119. # URL for this with a +prefix+
  120. def http_url(prefix)
  121. path = [prefix, @relative_name.tr('.', '_')]
  122. File.join(*path.compact) + '.html'
  123. end
  124. def inspect # :nodoc:
  125. "#<%s:0x%x %p modules: %p classes: %p>" % [
  126. self.class, object_id,
  127. base_name,
  128. @modules.map { |n,m| m },
  129. @classes.map { |n,c| c }
  130. ]
  131. end
  132. ##
  133. # Time this file was last modified, if known
  134. def last_modified
  135. @file_stat ? file_stat.mtime : nil
  136. end
  137. ##
  138. # Dumps this TopLevel for use by ri. See also #marshal_load
  139. def marshal_dump
  140. [
  141. MARSHAL_VERSION,
  142. @relative_name,
  143. @parser,
  144. parse(@comment),
  145. ]
  146. end
  147. ##
  148. # Loads this TopLevel from +array+.
  149. def marshal_load array # :nodoc:
  150. initialize array[1]
  151. @parser = array[2]
  152. @comment = array[3]
  153. @file_stat = nil
  154. end
  155. ##
  156. # Returns the NormalClass "Object", creating it if not found.
  157. #
  158. # Records +self+ as a location in "Object".
  159. def object_class
  160. @object_class ||= begin
  161. oc = @store.find_class_named('Object') || add_class(RDoc::NormalClass, 'Object')
  162. oc.record_location self
  163. oc
  164. end
  165. end
  166. ##
  167. # Base name of this file without the extension
  168. def page_name
  169. basename = File.basename @relative_name
  170. basename =~ /\.(rb|rdoc|txt|md)$/i
  171. $` || basename
  172. end
  173. ##
  174. # Path to this file for use with HTML generator output.
  175. def path
  176. http_url @store.rdoc.generator.file_dir
  177. end
  178. def pretty_print q # :nodoc:
  179. q.group 2, "[#{self.class}: ", "]" do
  180. q.text "base name: #{base_name.inspect}"
  181. q.breakable
  182. items = @modules.map { |n,m| m }
  183. items.concat @modules.map { |n,c| c }
  184. q.seplist items do |mod| q.pp mod end
  185. end
  186. end
  187. ##
  188. # Search record used by RDoc::Generator::JsonIndex
  189. def search_record
  190. return unless @parser < RDoc::Parser::Text
  191. [
  192. page_name,
  193. '',
  194. page_name,
  195. '',
  196. path,
  197. '',
  198. snippet(@comment),
  199. ]
  200. end
  201. ##
  202. # Is this TopLevel from a text file instead of a source code file?
  203. def text?
  204. @parser and @parser.ancestors.include? RDoc::Parser::Text
  205. end
  206. def to_s # :nodoc:
  207. "file #{full_name}"
  208. end
  209. end