Class: YARD::Server::LibraryVersion
Relationships & Source Files | |
Inherits: | Object |
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:
=> [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, 3 methods can be added to the LibraryVersion
class, #load_yardoc_from_SOURCE
, #yardoc_file_for_SOURCE
, and
#source_path_for_SOURCE
. In all cases, "SOURCE" represents the source
type used in #source when creating the library object. The
#yardoc_file_for_SOURCE
and #source_path_for_SOURCE
methods are 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.
Note that only #load_yardoc_from_SOURCE
is required. The other two
methods are optional and can be set manually (via #source_path= and
#yardoc_file=) on the object at any time.
Class Method Summary
Instance Attribute Summary
- #name ⇒ String rw
- #ready? ⇒ Boolean readonly
- #source ⇒ Symbol rw
- #source_path ⇒ String? rw
- #source_path=(value) rw
- #version ⇒ String rw
- #yardoc_file ⇒ String? rw
- #yardoc_file=(value) rw
Instance Method Summary
-
#==(other)
Alias for #eql?.
- #eql?(other) ⇒ Boolean (also: #==, #equal?)
-
#equal?(other)
Alias for #eql?.
- #gemspec ⇒ Gem::Specification?
- #hash ⇒ Fixnum
-
#prepare!
Prepares a library to be displayed by the server.
- #to_s(url_format = true) ⇒ String
- #load_source_path private
- #load_yardoc_file private
- #serializer private
Constructor Details
.new(name, version = nil, yardoc = nil, source = :disk) ⇒ LibraryVersion
Instance Attribute Details
#name ⇒ String (rw)
# File 'lib/yard/server/library_version.rb', line 96
attr_accessor :name
#ready? ⇒ Boolean
(readonly)
# File 'lib/yard/server/library_version.rb', line 162
def ready? return false if yardoc_file.nil? serializer.complete? end
#source ⇒ Symbol
(rw)
# File 'lib/yard/server/library_version.rb', line 116
attr_accessor :source
#source_path ⇒ String? (rw)
# File 'lib/yard/server/library_version.rb', line 122
def source_path @source_path ||= load_source_path end
#source_path=(value) (rw)
# File 'lib/yard/server/library_version.rb', line 125
attr_writer :source_path
#version ⇒ String (rw)
# File 'lib/yard/server/library_version.rb', line 99
attr_accessor :version
#yardoc_file ⇒ String? (rw)
To implement a custom yardoc file getter, implement
# File 'lib/yard/server/library_version.rb', line 106
def yardoc_file @yardoc_file ||= load_yardoc_file end
#yardoc_file=(value) (rw)
# File 'lib/yard/server/library_version.rb', line 109
attr_writer :yardoc_file
Instance Method Details
#==(other)
Alias for #eql?.
# File 'lib/yard/server/library_version.rb', line 157
alias == eql?
#eql?(other) ⇒ Boolean
Also known as: #==, #equal?
# File 'lib/yard/server/library_version.rb', line 153
def eql?(other) other.is_a?(LibraryVersion) && other.name == name && other.version == version && other.yardoc_file == yardoc_file end
#equal?(other)
Alias for #eql?.
# File 'lib/yard/server/library_version.rb', line 158
alias equal? eql?
#gemspec ⇒ Gem::Specification
?
#hash ⇒ Fixnum
# File 'lib/yard/server/library_version.rb', line 150
def hash; to_s.hash end
#load_source_path (private)
# File 'lib/yard/server/library_version.rb', line 261
def load_source_path meth = "source_path_for_#{source}" send(meth) if respond_to?(meth, true) end
#load_yardoc_file (private)
# File 'lib/yard/server/library_version.rb', line 266
def load_yardoc_file meth = "yardoc_file_for_#{source}" send(meth) if respond_to?(meth, true) end
#prepare!
You should not directly override this method. Instead, implement
load_yardoc_from_SOURCENAME
when implementing loading for a specific
source type. See the 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).
#serializer (private)
# File 'lib/yard/server/library_version.rb', line 271
def serializer return if yardoc_file.nil? Serializers::YardocSerializer.new(yardoc_file) end