Class: YARD::Handlers::Base Abstract
Overview
Subclass this class to provide a handler for ::YARD
to use
during the processing phase.
::YARD::Handlers
are pluggable semantic parsers for YARD's code generation
phase. They allow developers to control what information gets
generated by ::YARD
, giving them the ability to, for instance, document
any Ruby
DSLs that a customized framework may use. A good example
of this would be the ability to document and generate meta data for
the 'describe' declaration of the RSpec testing framework by simply
adding a handler for such a keyword. Similarly, any Ruby
API that
takes advantage of class level declarations could add these to the
documentation in a very explicit format by treating them as first-
class objects in any outputted documentation.
Overview of a Typical Handler Scenario
Generally, a handler class will declare a set of statements which it will handle using the .handles class declaration. It will then implement the #process method to do the work. The processing would usually involve the manipulation of the #namespace, #owner code objects or the creation of new ones, in which case they should be registered by #register, a method that sets some basic attributes for the new objects.
::YARD::Handlers
are usually simple and take up to a page of code to process
and register a new object or add new attributes to the current #namespace.
Setting up a Handler for Use
A Handler is automatically registered when it is subclassed from the
base class. The only other thing that needs to be done is to specify
which statement the handler will process. This is done with the .handles
declaration, taking either a ::YARD::Parser::Ruby::Legacy::RubyToken
, ::String
or Regexp
.
Here is a simple example which processes module statements.
class MyModuleHandler < YARD::Handlers::Base handles TkMODULE
def process
# do something
end
end
Processing Handler Data
The goal of a specific handler is really up to the developer, and as such there is no real guideline on how to process the data. However, it is important to know where the data is coming from to be able to use it.
#statement Attribute
The #statement attribute pertains to the ::YARD::Parser::Ruby::Legacy::Statement
object
containing a set of tokens parsed in by the parser. This is the main set
of data to be analyzed and processed. The comments attached to the statement
can be accessed by the Parser::Ruby::Legacy::Statement#comments method, but generally
the data to be processed will live in the tokens
attribute. This list
can be converted to a ::String
using #to_s
to parse the data with
regular expressions (or other text processing mechanisms), if needed.
#namespace Attribute
The #namespace attribute is a namespace object which represents the current namespace that the parser is in. For instance:
module SomeModule class MyClass def mymethod; end end end
If a handler was to parse the 'class MyClass' statement, it would be necessary to know that it belonged inside the SomeModule module. This is the value that #namespace would return when processing such a statement. If the class was then entered and another handler was called on the method, the #namespace would be set to the 'MyClass' code object.
#owner Attribute
The #owner attribute is similar to the #namespace attribute in that
it also follows the scope of the code during parsing. However, a namespace
object is loosely defined as a module or class and ::YARD
has the ability
to parse beyond module and class blocks (inside methods, for instance),
so the #owner attribute would not be limited to modules and classes.
To put this into context, the example from above will be used. If a method handler was added to the mix and decided to parse inside the method body, the #owner would be set to the method object but the namespace would remain set to the class. This would allow the developer to process any method definitions set inside a method (def x; def y; 2 end end) by adding them to the correct namespace (the class, not the method).
In summary, the distinction between #namespace and #owner can be thought
of as the difference between first-class Ruby
objects (namespaces) and
second-class Ruby
objects (methods).
#visibility and #scope Attributes
Mainly needed for parsing methods, the #visibility and #scope attributes refer to the public/protected/private and class/instance values (respectively) of the current parsing position.
Parsing Blocks in Statements
In addition to parsing a statement and creating new objects, some handlers may wish to continue parsing the code inside the statement's block (if there is one). In this context, a block means the inside of any statement, be it class definition, module definition, if statement or classic 'Ruby block'.
For example, a class statement would be "class MyClass" and the block would be a list of statements including the method definitions inside the class. For a class handler, the programmer would execute the #parse_block method to continue parsing code inside the block, with the #namespace now pointing to the class object the handler created.
::YARD
has the ability to continue into any block: class, module, method,
even if statements. For this reason, the block parsing method must be
invoked explicitly out of efficiency sake.
Constant Summary
::YARD::CodeObjects
- Included
BUILTIN_ALL, BUILTIN_CLASSES, BUILTIN_EXCEPTIONS, BUILTIN_EXCEPTIONS_HASH, BUILTIN_MODULES, CONSTANTMATCH, CONSTANTSTART, CSEP, CSEPQ, ISEP, ISEPQ, METHODMATCH, METHODNAMEMATCH, NAMESPACEMATCH, NSEP, NSEPQ, PROXY_MATCH
Macro Support
- #call_params ⇒ Array<String> abstract
- #caller_method ⇒ String? abstract
Class Attribute Summary
-
.namespace_only ⇒ void
readonly
Declares that the handler should only be called when inside a
::YARD::CodeObjects::NamespaceObject
, not a method body. - .namespace_only? ⇒ Boolean readonly
Class Method Summary
-
.clear_subclasses ⇒ void
Clear all registered subclasses.
- .handlers ⇒ Array
-
.handles(*matches)
Declares the statement type which will be processed by this handler.
-
.handles?(statement) ⇒ Boolean
This class is implemented by
Ruby::Base
andRuby::Legacy::Base
. -
.in_file(filename) ⇒ void
Declares that a handler should only be called when inside a filename by its basename or a regex match for the full path.
- .inherited(subclass)
- .matches_file?(filename) ⇒ Boolean
- .new(source_parser, stmt) ⇒ Base constructor
-
.process(&block) ⇒ void
Generates a .process method, equivalent to +def process; ...
-
.subclasses ⇒ Array<Base>
Returns all registered handler subclasses.
Instance Attribute Summary
-
#extra_state ⇒ OpenStruct
readonly
Share state across different handlers inside of a file.
-
#globals ⇒ OpenStruct
readonly
::YARD::Handlers
can share state for the entire post processing stage through this attribute. - #namespace ⇒ CodeObjects::NamespaceObject rw
- #namespace=(v) ⇒ CodeObjects::NamespaceObject rw
- #owner ⇒ CodeObjects::Base? rw
- #owner=(v) ⇒ CodeObjects::Base? rw
- #parser ⇒ Processor readonly
- #scope ⇒ Symbol rw
- #scope=(v) ⇒ Symbol rw
- #statement ⇒ Object readonly
- #visibility ⇒ Symbol rw
- #visibility=(v) ⇒ Symbol rw
Instance Method Summary
-
#abort!
Aborts a handler by raising
HandlerAborted
. -
#ensure_loaded!(object, max_retries = 1)
Ensures that a specific
object
has been parsed and loaded into the registry. -
#parse_block
abstract
Parses the semantic "block" contained in the statement node.
-
#process ⇒ Array<CodeObjects::Base>, ...
The main handler method called by the parser on a statement that matches the .handles declaration.
-
#push_state(opts = {}) { ... }
Executes a given block with specific state values for #owner, #namespace and #scope.
-
#register(*objects) ⇒ CodeObjects::Base+
Do some post processing on a list of code objects.
-
#register_docstring(object, docstring = statement.comments, stmt = statement) ⇒ void
Registers any docstring found for the object and expands macros.
-
#register_dynamic(object) ⇒ void
Registers the object as dynamic if the object is defined inside a method or block (owner != namespace).
-
#register_ensure_loaded(object) ⇒ void
Ensures that the object's namespace is loaded before attaching it to the namespace.
-
#register_file_info(object, file = parser.file, line = statement.line, comments = statement.comments) ⇒ void
Registers the file/line of the declaration with the object.
-
#register_group(object, group = extra_state.group) ⇒ void
Registers the object as being inside a specific group.
-
#register_module_function(object)
Registers the same method information on the module function, if the object was defined as a module function.
- #register_source(object, source = statement, type = parser.parser_type) ⇒ void
-
#register_transitive_tags(object) ⇒ void
Registers any transitive tags from the namespace on the object.
-
#register_visibility(object, visibility = self.visibility)
Registers visibility on a method object.
Constructor Details
.new(source_parser, stmt) ⇒ Base
# File 'lib/yard/handlers/base.rb', line 276
def initialize(source_parser, stmt) @parser = source_parser @statement = stmt end
Class Attribute Details
.namespace_only ⇒ void
(readonly)
This method returns an undefined value.
Declares that the handler should only be called when inside a
::YARD::CodeObjects::NamespaceObject
, not a method body.
# File 'lib/yard/handlers/base.rb', line 219
def namespace_only @namespace_only = true end
.namespace_only? ⇒ Boolean
(readonly)
# File 'lib/yard/handlers/base.rb', line 225
def namespace_only? @namespace_only ||= false end
Class Method Details
.clear_subclasses ⇒ void
This method returns an undefined value.
Clear all registered subclasses. Testing purposes only
# File 'lib/yard/handlers/base.rb', line 159
def clear_subclasses @@subclasses = [] end
.handlers ⇒ Array
# File 'lib/yard/handlers/base.rb', line 211
def handlers @handlers ||= [] end
.handles(*matches)
Declares the statement type which will be processed by this handler.
A match need not be unique to a handler. Multiple
handlers can process the same statement. However,
in this case, care should be taken to make sure that
#parse_block would only be executed by one of
the handlers, otherwise the same code will be parsed
multiple times and slow ::YARD
down.
# File 'lib/yard/handlers/base.rb', line 192
def handles(*matches) (@handlers ||= []).concat(matches) end
.handles?(statement) ⇒ Boolean
This class is implemented by Ruby::Base
and Ruby::Legacy::Base
.
To implement a base handler class for another language, implement
this method to return true if the handler should process the given
statement object. Use .handlers to enumerate the matchers declared
for the handler class.
# File 'lib/yard/handlers/base.rb', line 205
def handles?(statement) # rubocop:disable Lint/UnusedMethodArgument raise NotImplementedError, "override #handles? in a subclass" end
.in_file(filename) ⇒ void
This method returns an undefined value.
Declares that a handler should only be called when inside a filename by its basename or a regex match for the full path.
# File 'lib/yard/handlers/base.rb', line 235
def in_file(filename) (@in_files ||= []) << filename end
.inherited(subclass)
[ GitHub ]# File 'lib/yard/handlers/base.rb', line 169
def inherited(subclass) @@subclasses ||= [] @@subclasses << subclass end
.matches_file?(filename) ⇒ Boolean
.process(&block) ⇒ void
This method returns an undefined value.
Generates a process
method, equivalent to +def process; ... end+.
Blocks defined with this syntax will be wrapped inside an anonymous
module so that the handler class can be extended with mixins that
override the process
method without alias chaining.
.subclasses ⇒ Array<Base
>
Returns all registered handler subclasses.
# File 'lib/yard/handlers/base.rb', line 165
def subclasses @@subclasses ||= [] end
Instance Attribute Details
#extra_state ⇒ OpenStruct (readonly)
Share state across different handlers inside of a file. This attribute is similar to #visibility, #scope, #namespace and #owner, in that they all maintain state across all handlers for the entire source file. Use this attribute to store any data your handler might need to save during the parsing of a file. If you need to save state across files, see #globals.
# File 'lib/yard/handlers/base.rb', line 348
attr_reader :extra_state
#globals ⇒ OpenStruct (readonly)
::YARD::Handlers
can share state for the entire post processing stage through
this attribute. Note that post processing stage spans multiple files.
To share state only within a single file, use #extra_state
# File 'lib/yard/handlers/base.rb', line 347
attr_reader :globals
#namespace ⇒ CodeObjects::NamespaceObject (rw)
# File 'lib/yard/handlers/base.rb', line 341
attr_accessor :namespace
#namespace=(v) ⇒ CodeObjects::NamespaceObject (rw)
# File 'lib/yard/handlers/base.rb', line 342
attr_accessor :namespace
#owner ⇒ CodeObjects::Base? (rw)
# File 'lib/yard/handlers/base.rb', line 339
attr_accessor :owner
#owner=(v) ⇒ CodeObjects::Base? (rw)
# File 'lib/yard/handlers/base.rb', line 340
attr_accessor :owner
#parser ⇒ Processor (readonly)
# File 'lib/yard/handlers/base.rb', line 310
attr_reader :parser
#scope ⇒ Symbol
(rw)
# File 'lib/yard/handlers/base.rb', line 345
attr_accessor :scope
#scope=(v) ⇒ Symbol
(rw)
# File 'lib/yard/handlers/base.rb', line 346
attr_accessor :scope
#statement ⇒ Object
(readonly)
# File 'lib/yard/handlers/base.rb', line 315
attr_reader :statement
#visibility ⇒ Symbol
(rw)
# File 'lib/yard/handlers/base.rb', line 343
attr_accessor :visibility
#visibility=(v) ⇒ Symbol
(rw)
# File 'lib/yard/handlers/base.rb', line 344
attr_accessor :visibility
Instance Method Details
#abort!
Aborts a handler by raising HandlerAborted
.
An exception will only be logged in debugging mode for
this kind of handler exit.
# File 'lib/yard/handlers/base.rb', line 355
def abort! raise Handlers::HandlerAborted end
#call_params ⇒ Array<String>
Implement this method to return the parameters in a method call statement. It should return an empty list if the statement is not a method call.
# File 'lib/yard/handlers/base.rb', line 581
def call_params raise NotImplementedError end
#caller_method ⇒ String?
Implement this method to return the method being called in a method call. It should return nil if the statement is not a method call.
# File 'lib/yard/handlers/base.rb', line 590
def caller_method raise NotImplementedError end
#ensure_loaded!(object, max_retries = 1)
Ensures that a specific object
has been parsed and loaded into the
registry. This is necessary when adding data to a namespace, for instance,
since the namespace may not have been processed yet (it can be located
in a file that has not been handled).
Calling this method defers the handler until all other files have been processed. If the object gets resolved, the rest of the handler continues, otherwise an exception is raised.
# File 'lib/yard/handlers/base.rb', line 561
def ensure_loaded!(object, max_retries = 1) return if object.root? return object unless object.is_a?(Proxy) retries = 0 while object.is_a?(Proxy) raise NamespaceMissingError, object if retries > max_retries log.debug "Missing object #{object} in file `#{parser.file}', moving it to the back of the line." parser.parse_remaining_files retries += 1 end object end
#parse_block
Subclasses should call parser.process
Parses the semantic "block" contained in the statement node.
# File 'lib/yard/handlers/base.rb', line 304
def parse_block(*) raise NotImplementedError, "#{self} did not implement a #parse_block method for handling" end
#process ⇒ Array<CodeObjects::Base>, ...
The main handler method called by the parser on a statement that matches the .handles declaration.
Subclasses should override this method to provide the handling functionality for the class.
# File 'lib/yard/handlers/base.rb', line 297
def process raise NotImplementedError, "#{self} did not implement a #process method for handling." end
#push_state(opts = {}) { ... }
Executes a given block with specific state values for #owner, #namespace and #scope.
# File 'lib/yard/handlers/base.rb', line 370
def push_state(opts = {}) opts = { :namespace => namespace, :scope => :instance, :owner => owner || namespace, :visibility => nil }.update(opts) ns = namespace vis = visibility sc = scope oo = owner self.namespace = opts[:namespace] self.visibility = opts[:visibility] || :public self.scope = opts[:scope] self.owner = opts[:owner] yield self.namespace = ns self.visibility = vis self.scope = sc self.owner = oo end
#register(*objects) ⇒ CodeObjects::Base+
Do some post processing on a list of code objects. Adds basic attributes to the list of objects like the filename, line number, CodeObjects::Base#dynamic, source code and CodeObjects::Base#docstring, but only if they don't exist.
# File 'lib/yard/handlers/base.rb', line 407
def register(*objects) objects.flatten.each do |object| next unless object.is_a?(CodeObjects::Base) register_ensure_loaded(object) yield(object) if block_given? register_file_info(object) register_source(object) register_visibility(object) register_docstring(object) register_group(object) register_dynamic(object) register_module_function(object) end objects.size == 1 ? objects.first : objects end
#register_docstring(object, docstring = statement.comments, stmt = statement) ⇒ void
This method returns an undefined value.
Registers any docstring found for the object and expands macros
# File 'lib/yard/handlers/base.rb', line 450
def register_docstring(object, docstring = statement.comments, stmt = statement) docstring = docstring.join("\n") if Array === docstring parser = Docstring.parser parser.parse(docstring || "", object, self) if object && docstring object.docstring = parser.to_docstring # Add hash_flag/line_range if stmt object.docstring.hash_flag = stmt.comments_hash_flag object.docstring.line_range = stmt.comments_range end end (object) end
#register_dynamic(object) ⇒ void
This method returns an undefined value.
Registers the object as dynamic if the object is defined inside a method or block (owner != namespace)
#register_ensure_loaded(object) ⇒ void
This method returns an undefined value.
Ensures that the object's namespace is loaded before attaching it to the namespace.
# File 'lib/yard/handlers/base.rb', line 429
def register_ensure_loaded(object) ensure_loaded!(object.namespace) object.namespace.children << object rescue NamespaceMissingError nil # noop end
#register_file_info(object, file = parser.file, line = statement.line, comments = statement.comments) ⇒ void
This method returns an undefined value.
Registers the file/line of the declaration with the object
#register_group(object, group = extra_state.group) ⇒ void
This method returns an undefined value.
Registers the object as being inside a specific group
# File 'lib/yard/handlers/base.rb', line 473
def register_group(object, group = extra_state.group) if group unless object.namespace.is_a?(Proxy) object.namespace.groups |= [group] end object.group = group end end
#register_module_function(object)
Registers the same method information on the module function, if the object was defined as a module function.
# File 'lib/yard/handlers/base.rb', line 523
def register_module_function(object) return unless object.is_a?(MethodObject) return unless object.module_function? modobj = MethodObject.new(object.namespace, object.name) object.copy_to(modobj) modobj.visibility = :private # rubocop:disable Lint/UselessSetterCall end
#register_source(object, source = statement, type = parser.parser_type) ⇒ void
This method returns an undefined value.
# File 'lib/yard/handlers/base.rb', line 499
def register_source(object, source = statement, type = parser.parser_type) return unless object.is_a?(MethodObject) object.source ||= source object.source_type = type end
#register_transitive_tags(object) ⇒ void
This method returns an undefined value.
Registers any transitive tags from the namespace on the object
#register_visibility(object, visibility = self.visibility)
Registers visibility on a method object. If the object does not respond to setting visibility, nothing is done.
# File 'lib/yard/handlers/base.rb', line 511
def register_visibility(object, visibility = self.visibility) return unless object.respond_to?(:visibility=) return if object.is_a?(NamespaceObject) object.visibility = visibility end