Class: YARD::Server::LibraryVersion
- Inherits:
-
Object
- Object
- YARD::Server::LibraryVersion
- Defined in:
- lib/yard/server/library_version.rb
Overview
A library version encapsulates a library's documentation at a specific version. Although the version is optional, this allows for creating multiple documentation points for a specific library, each representing a unique version. The term "library" used in other parts of the YARD::Server documentation refers to objects of this class unless otherwise noted.
A library points to a location where a #yardoc_file is located so that its documentation may be loaded and served. Optionally, a #source_path is given to point to a location where any extra files (and .yardopts) should be loaded from. Both of these methods may not be known immediately, since the yardoc file may not be built until later. Resolving the yardoc file and source path are dependent on the specific library "source type" used. Source types (known as "library source") are discussed in detail below.
Using with Adapters
A list of libraries need to be passed into adapters upon creation. In most cases, you will never do this manually, but if you use a RackMiddleware, you will need to pass in this list yourself. To build this list of libraries, you should create a hash of library names mapped to an Array of LibraryVersion objects. For example:
{'mylib' => [LibraryVersion.new('mylib', '1.0', ...), LibraryVersion.new('mylib', '2.0', ...)]}
Note that you can also use Adapter#add_library for convenience.
The "array" part is required, even for just one library version.
Library Sources
The #source method represents the library source type, ie. where the library "comes from". It might come from "disk", or it might come from a "gem" (technically the disk, but a separate type nonetheless). In these two cases, the yardoc file sits somewhere on your filesystem, though it may also be built dynamically if it does not yet exist. This behaviour is controlled through the #prepare! method, which prepares the yardoc file given a specific library source. We will see how this works in detail in the following section.
Implementing a Custom Library Source
YARD can be extended to support custom library sources in order to build or retrieve a yardoc file at runtime from many different locations.
To implement this behaviour, two methods must be added to the LibraryVersion class, #load_yardoc_from_SOURCE and #source_path_for_SOURCE. In both cases, "SOURCE" represents the source type used in #source when creating the library object. The #source_path_for_SOURCE method is called upon creation and should return the location where the source code for the library lives. The load method is called from #prepare! if there is no yardoc file and should set #yardoc_file. Below is a full example for implementing a custom library source, :http, which reads packaged .yardoc databases from zipped archives off of an HTTP server.
Instance Attribute Summary (collapse)
-
- (String) name
The name of the library.
-
- (Symbol) source
The source type representing where the yardoc should be loaded from.
- - (String?) source_path
-
- (String) version
The version of the specific library.
- - (String?) yardoc_file
Instance Method Summary (collapse)
-
- (Boolean) eql?(other)
(also: #==, #equal?)
Whether another LibraryVersion is equal to this one.
- - (Gem::Specification?) gemspec
-
- (Fixnum) hash
Used for Hash mapping.
-
- (LibraryVersion) initialize(name, version = nil, yardoc = nil, source = :disk)
constructor
A new instance of LibraryVersion.
-
- (Object) load_yardoc_from_disk
protected
Called when a library of source type "disk" is to be prepared.
-
- (Object) load_yardoc_from_gem
protected
Called when a library of source type "gem" is to be prepared.
-
- (Object) prepare!
Prepares a library to be displayed by the server.
-
- (String) source_path_for_disk
protected
The source path for a disk source.
-
- (String) source_path_for_gem
protected
The source path for a gem source.
-
- (String) to_s(url_format = true)
The string representation of the library.
Constructor Details
- (LibraryVersion) initialize(name, version = nil, yardoc = nil, source = :disk)
A new instance of LibraryVersion
120 121 122 123 124 125 126 |
# File 'lib/yard/server/library_version.rb', line 120 def initialize(name, version = nil, yardoc = nil, source = :disk) self.name = name self.yardoc_file = yardoc self.version = version self.source = source self.source_path = load_source_path end |
Instance Attribute Details
- (String) name
The name of the library
89 90 91 |
# File 'lib/yard/server/library_version.rb', line 89 def name @name end |
- (Symbol) source
The source type representing where the yardoc should be loaded from. Defaults are :disk and :gem, though custom sources may be implemented. This value is used to inform #prepare! about how to load the necessary data in order to display documentation for an object.
105 106 107 |
# File 'lib/yard/server/library_version.rb', line 105 def source @source end |
- (String?) source_path
111 112 113 |
# File 'lib/yard/server/library_version.rb', line 111 def source_path @source_path end |
- (String) version
The version of the specific library
92 93 94 |
# File 'lib/yard/server/library_version.rb', line 92 def version @version end |
- (String?) yardoc_file
98 99 100 |
# File 'lib/yard/server/library_version.rb', line 98 def yardoc_file @yardoc_file end |
Instance Method Details
- (Boolean) eql?(other) Also known as: ==, equal?
Whether another LibraryVersion is equal to this one
140 141 142 143 |
# File 'lib/yard/server/library_version.rb', line 140 def eql?(other) other.is_a?(LibraryVersion) && other.name == name && other.version == version && other.yardoc_file == yardoc_file end |
- (Gem::Specification?) gemspec
171 172 173 174 |
# File 'lib/yard/server/library_version.rb', line 171 def gemspec ver = version ? "= #{version}" : ">= 0" Gem.source_index.find_name(name, ver).first end |
- (Fixnum) hash
Used for Hash mapping.
137 |
# File 'lib/yard/server/library_version.rb', line 137 def hash; to_s.hash end |
- (Object) load_yardoc_from_disk (protected)
Called when a library of source type "disk" is to be prepared. In this case, the #yardoc_file should already be set, so nothing needs to be done.
181 182 183 |
# File 'lib/yard/server/library_version.rb', line 181 def load_yardoc_from_disk nil end |
- (Object) load_yardoc_from_gem (protected)
Called when a library of source type "gem" is to be prepared. In this case, the #yardoc_file needs to point to the correct location for the installed gem. The yardoc file is built if it has not been done.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/yard/server/library_version.rb', line 191 def load_yardoc_from_gem require 'rubygems' ver = version ? "= #{version}" : ">= 0" self.yardoc_file = Registry.yardoc_file_for_gem(name, ver) unless yardoc_file && File.directory?(yardoc_file) Thread.new do # Build gem docs on demand log.debug "Building gem docs for #{to_s(false)}" CLI::Gems.run(name, ver) self.yardoc_file = Registry.yardoc_file_for_gem(name, ver) FileUtils.touch(File.join(yardoc_file, 'complete')) end end unless yardoc_file && File.exist?(File.join(yardoc_file, 'complete')) raise LibraryNotPreparedError end end |
- (Object) prepare!
You should not directly override this method. Instead, implement load_yardoc_from_SOURCENAME when implementing loading for a specific source type. See the YARD::Server::LibraryVersion documentation for "Implementing a Custom Library Source"
Prepares a library to be displayed by the server. This callback is performed before each request on a library to ensure that it is loaded and ready to be viewed. If any steps need to be performed prior to loading, they are performed through this method (though they should be implemented through the load_yardoc_from_SOURCE method).
162 163 164 165 166 |
# File 'lib/yard/server/library_version.rb', line 162 def prepare! return if yardoc_file meth = "load_yardoc_from_#{source}" send(meth) if respond_to?(meth) end |
- (String) source_path_for_disk (protected)
The source path for a disk source
210 211 212 |
# File 'lib/yard/server/library_version.rb', line 210 def source_path_for_disk File.dirname(yardoc_file) if yardoc_file end |
- (String) source_path_for_gem (protected)
The source path for a gem source
215 216 217 |
# File 'lib/yard/server/library_version.rb', line 215 def source_path_for_gem gemspec.full_gem_path if gemspec end |
- (String) to_s(url_format = true)
The string representation of the library.
132 133 134 |
# File 'lib/yard/server/library_version.rb', line 132 def to_s(url_format = true) version ? "#{name}#{url_format ? '/' : '-'}#{version}" : "#{name}" end |