Class: YARD::Tags::Library
- Inherits:
-
Object
- Object
- YARD::Tags::Library
- Defined in:
- lib/yard/tags/library.rb
Overview
Keeps track of all the registered meta-data tags and directives. Also allows for defining of custom tags and customizing the tag parsing syntax.
Defining Custom Meta-Data Tags
To define a custom tag, use Library.define_tag. You should pass the tag name and the factory method to use when creating the tag. If you do not provide a factory method to use, it will default to DefaultFactory#parse_tag
You can also define tag objects manually by simply implementing a “tagname_tag” method that returns a Tag object, but they will not take advantage of tag factory parsing:
def mytag_tag(text)
Tag.new(:mytag, text)
end
Defining Custom Directives
Directives can be defined by calling the Library.define_directive method, taking the directive name, an optional tag factory parser method (to parse the data in the directive into a temporary Tag object) and a Directive subclass that performs the directive processing. For more information on creating a Directive subclass, see the Directive class documentation.
Similar to tags, Directives can also be defined manually, in this case using the method name “mydirective_directive” and returning a new Directive object:
def mydirective_directive(tag, parser)
MyDirective.new(tag, parser)
end
Namespaced Tags
In YARD 0.8.0+, tags can be namespaced using the ‘.’ character. It is recommended to namespace project specific tags, like @yard.tag_name, so that tags do not collide with other plugins or new built-in tags.
Adding/Changing the Tag Syntax
If you have specialized tag parsing needs you can substitute the #factory object with your own by setting Library.default_factory to a new class with its own parsing methods before running YARD. This is useful if you want to change the syntax of existing tags (@see, @since, etc.)
Class Attribute Summary collapse
-
.labels ⇒ Object
readonly
Returns the value of attribute labels.
-
.transitive_tags ⇒ Array<Symbol>
Sets the list of tags that should apply to any children inside the namespace they are defined in.
-
.visible_tags ⇒ Array<Symbol>
Sets the list of tags to display when rendering templates.
Instance Attribute Summary collapse
-
#factory ⇒ Object
A factory class to handle parsing of tags, defaults to Library.default_factory.
Class Method Summary collapse
- .default_factory ⇒ Object
-
.default_factory=(factory) ⇒ Object
Replace the factory object responsible for parsing tags by setting this to an object (or class) that responds to
parse_TAGNAME
methods whereTAGNAME
is the name of the tag. - .define_directive(tag, tag_meth = nil, directive_class) ⇒ Object
-
.define_tag(label, tag, meth = nil) ⇒ Object
Convenience method to define a new tag using one of Tag‘s factory methods, or the regular DefaultFactory#parse_tag factory method if none is supplied.
- .directive_method_name(tag_name) ⇒ Object
-
.factory_method_for(tag) ⇒ Symbol, ...
Returns the factory method used to parse the tag text for a specific tag.
-
.factory_method_for_directive(directive) ⇒ Symbol, ...
Returns the factory method used to parse the tag text for a specific directive.
- .instance ⇒ Object
-
.sorted_labels ⇒ Array<Symbol>, String
Sorts the labels lexically by their label name, often used when displaying the tags.
- .tag_method_name(tag_name) ⇒ Object
Instance Method Summary collapse
- #directive_create(tag_name, tag_buf, parser) ⇒ Directive
- #has_directive?(tag_name) ⇒ Boolean
- #has_tag?(tag_name) ⇒ Boolean
-
#initialize(factory = Library.default_factory) ⇒ Library
constructor
A new instance of Library.
- #tag_create(tag_name, tag_buf) ⇒ Object
Constructor Details
#initialize(factory = Library.default_factory) ⇒ Library
Returns a new instance of Library.
255 256 257 |
# File 'lib/yard/tags/library.rb', line 255 def initialize(factory = Library.default_factory) self.factory = factory end |
Class Attribute Details
.labels ⇒ Object (readonly)
Returns the value of attribute labels.
60 61 62 |
# File 'lib/yard/tags/library.rb', line 60 def labels @labels end |
.transitive_tags ⇒ Array<Symbol>
Sets the list of tags that should apply to any children inside the namespace they are defined in. For instance, a “@since” tag should apply to all methods inside a module it is defined in. Transitive tags can be overridden by directly defining a tag on the child object.
132 133 134 |
# File 'lib/yard/tags/library.rb', line 132 def @transitive_tags end |
.visible_tags ⇒ Array<Symbol>
Sets the list of tags to display when rendering templates. The order of tags in the list is also significant, as it represents the order that tags are displayed in templates.
You can use the Array#place to insert new tags to be displayed in the templates at specific positions:
Library..place(:mytag).before(:return)
123 124 125 |
# File 'lib/yard/tags/library.rb', line 123 def @visible_tags end |
Instance Attribute Details
#factory ⇒ Object
A factory class to handle parsing of tags, defaults to default_factory
253 254 255 |
# File 'lib/yard/tags/library.rb', line 253 def factory @factory end |
Class Method Details
.default_factory ⇒ Object
66 67 68 |
# File 'lib/yard/tags/library.rb', line 66 def default_factory @default_factory ||= DefaultFactory.new end |
.default_factory=(factory) ⇒ Object
Replace the factory object responsible for parsing tags by setting this to an object (or class) that responds to parse_TAGNAME
methods where TAGNAME
is the name of the tag.
You should set this value before performing any source parsing with YARD, otherwise your factory class will not be used.
83 84 85 |
# File 'lib/yard/tags/library.rb', line 83 def default_factory=(factory) @default_factory = factory.is_a?(Class) ? factory.new : factory end |
.define_directive(tag, tag_meth = nil, directive_class) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/yard/tags/library.rb', line 191 def define_directive(tag, tag_meth = nil, directive_class = nil) directive_meth = directive_method_name(tag) if directive_class.nil? tag_meth, directive_class = nil, tag_meth end class_eval <<-eof, __FILE__, __LINE__ def #{directive_meth}(tag, parser) directive_call(tag, parser) end eof @factory_methods ||= SymbolHash.new(false) @factory_methods.update(tag => tag_meth) @directive_factory_classes ||= SymbolHash.new(false) @directive_factory_classes.update(tag => directive_class) tag end |
.define_tag(label, tag, meth = nil) ⇒ Object
Convenience method to define a new tag using one of Tag‘s factory methods, or the regular DefaultFactory#parse_tag factory method if none is supplied.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/yard/tags/library.rb', line 153 def define_tag(label, tag, meth = nil) tag_meth = tag_method_name(tag) if meth.is_a?(Class) && Tag > meth class_eval <<-eof, __FILE__, __LINE__ def #{tag_meth}(text) #{meth}.new(#{tag.inspect}, text) end eof else class_eval <<-eof, __FILE__, __LINE__ def #{tag_meth}(text) send_to_factory(#{tag.inspect}, #{meth.inspect}, text) end eof end @labels ||= SymbolHash.new(false) @labels.update(tag => label) @factory_methods ||= SymbolHash.new(false) @factory_methods.update(tag => meth) tag end |
.directive_method_name(tag_name) ⇒ Object
214 215 216 |
# File 'lib/yard/tags/library.rb', line 214 def directive_method_name(tag_name) tag_or_directive_method_name(tag_name, 'directive') end |
.factory_method_for(tag) ⇒ Symbol, ...
Returns the factory method used to parse the tag text for a specific tag
95 96 97 |
# File 'lib/yard/tags/library.rb', line 95 def factory_method_for(tag) @factory_methods[tag] end |
.factory_method_for_directive(directive) ⇒ Symbol, ...
Returns the factory method used to parse the tag text for a specific directive
108 109 110 |
# File 'lib/yard/tags/library.rb', line 108 def factory_method_for_directive(directive) @directive_factory_classes[directive] end |
.instance ⇒ Object
62 63 64 |
# File 'lib/yard/tags/library.rb', line 62 def instance @instance ||= new end |
.sorted_labels ⇒ Array<Symbol>, String
Sorts the labels lexically by their label name, often used when displaying the tags.
138 139 140 |
# File 'lib/yard/tags/library.rb', line 138 def sorted_labels labels.sort_by {|a| a.last.downcase } end |
.tag_method_name(tag_name) ⇒ Object
210 211 212 |
# File 'lib/yard/tags/library.rb', line 210 def tag_method_name(tag_name) tag_or_directive_method_name(tag_name) end |
Instance Method Details
#directive_create(tag_name, tag_buf, parser) ⇒ Directive
272 273 274 275 276 277 |
# File 'lib/yard/tags/library.rb', line 272 def directive_create(tag_name, tag_buf, parser) meth = self.class.factory_method_for(tag_name) tag = send_to_factory(tag_name, meth, tag_buf) meth = self.class.directive_method_name(tag_name) send(meth, tag, parser) end |
#has_directive?(tag_name) ⇒ Boolean
267 268 269 |
# File 'lib/yard/tags/library.rb', line 267 def has_directive?(tag_name) tag_name && respond_to?(self.class.directive_method_name(tag_name)) end |
#has_tag?(tag_name) ⇒ Boolean
259 260 261 |
# File 'lib/yard/tags/library.rb', line 259 def has_tag?(tag_name) tag_name && respond_to?(self.class.tag_method_name(tag_name)) end |
#tag_create(tag_name, tag_buf) ⇒ Object
263 264 265 |
# File 'lib/yard/tags/library.rb', line 263 def tag_create(tag_name, tag_buf) send(self.class.tag_method_name(tag_name), tag_buf) end |