Class: YARD::Handlers::Processor
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/yard/handlers/processor.rb |
Overview
Iterates over all statements in a file and delegates them to the
Base
objects that are registered to handle the statement.
This class is passed to each handler and keeps overall processing state. For example, if the #visibility is set in a handler, all following statements will have access to this state. This allows "public", "protected" and "private" statements to be handled in classes and modules. In addition, the #namespace can be set during parsing to control where objects are being created from. You can also access extra stateful properties that any handler can set during the duration of the post processing of a file from #extra_state. If you need to access state across different files, look at #globals.
Class Attribute Summary
- .namespace_for_handler ⇒ Hash readonly Internal use only Internal use only
Class Method Summary
-
.new(parser) ⇒ Processor
constructor
Creates a new
Processor
for a #file. -
.register_handler_namespace(type, ns)
Registers a new namespace for handlers of the given type.
Instance Attribute Summary
-
#extra_state ⇒ OpenStruct
rw
Share state across different handlers inside of a file.
- #file ⇒ String rw
-
#globals ⇒ OpenStruct
rw
::YARD::Handlers
can share state for the entire post processing stage through this attribute. - #namespace ⇒ CodeObjects::NamespaceObject rw
- #owner ⇒ CodeObjects::Base? rw
- #parser_type ⇒ Symbol rw
- #scope ⇒ Symbol rw
- #visibility ⇒ Symbol rw
Instance Method Summary
-
#find_handlers(statement) ⇒ Array<Base>
Searches for all handlers in Base.subclasses that match the
statement
-
#parse_remaining_files ⇒ void
Continue parsing the remainder of the files in the
globals.ordered_parser
object. -
#process(statements) ⇒ void
Processes a list of statements by finding handlers to process each one.
-
#handler_base_class ⇒ Base
private
Returns the handler base class.
-
#handler_base_namespace ⇒ Module
private
The module holding the handlers to be loaded.
- #handles?(handler, statement) ⇒ Boolean private
-
#load_handlers ⇒ void
private
Loads handlers from #handler_base_namespace.
Constructor Details
.new(parser) ⇒ Processor
Creates a new Processor
for a #file.
# File 'lib/yard/handlers/processor.rb', line 91
def initialize(parser) @file = parser.file || "(stdin)" @namespace = YARD::Registry.root @visibility = :public @scope = :instance @owner = @namespace @parser_type = parser.parser_type @handlers_loaded = {} @globals = parser.globals || OpenStruct.new @extra_state = OpenStruct.new load_handlers end
Class Attribute Details
.namespace_for_handler ⇒ Hash (readonly)
# File 'lib/yard/handlers/processor.rb', line 32
attr_reader :namespace_for_handler
Class Method Details
.register_handler_namespace(type, ns)
Registers a new namespace for handlers of the given type.
# File 'lib/yard/handlers/processor.rb', line 23
def register_handler_namespace(type, ns) namespace_for_handler[type] = ns end
Instance Attribute Details
#extra_state ⇒ OpenStruct (rw)
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/processor.rb', line 87
attr_accessor :extra_state
#file ⇒ String (rw)
# File 'lib/yard/handlers/processor.rb', line 40
attr_accessor :file
#globals ⇒ OpenStruct (rw)
::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/processor.rb', line 76
attr_accessor :globals
#namespace ⇒ CodeObjects::NamespaceObject (rw)
# File 'lib/yard/handlers/processor.rb', line 43
attr_accessor :namespace
#owner ⇒ CodeObjects::Base? (rw)
# File 'lib/yard/handlers/processor.rb', line 55
attr_accessor :owner
#parser_type ⇒ Symbol
(rw)
# File 'lib/yard/handlers/processor.rb', line 58
attr_accessor :parser_type
#scope ⇒ Symbol
(rw)
# File 'lib/yard/handlers/processor.rb', line 49
attr_accessor :scope
#visibility ⇒ Symbol
(rw)
# File 'lib/yard/handlers/processor.rb', line 46
attr_accessor :visibility
Instance Method Details
#find_handlers(statement) ⇒ Array<Base>
Searches for all handlers in Base.subclasses that match the statement
# File 'lib/yard/handlers/processor.rb', line 150
def find_handlers(statement) Base.subclasses.find_all do |handler| handler_base_class > handler && (handler.namespace_only? ? owner.is_a?(CodeObjects::NamespaceObject) : true) && handles?(handler, statement) end end
#handler_base_class ⇒ Base (private)
Returns the handler base class
# File 'lib/yard/handlers/processor.rb', line 171
def handler_base_class handler_base_namespace.const_get(:Base) end
#handler_base_namespace ⇒ Module (private)
The module holding the handlers to be loaded
# File 'lib/yard/handlers/processor.rb', line 179
def handler_base_namespace self.class.namespace_for_handler[parser_type] end
#handles?(handler, statement) ⇒ Boolean
(private)
# File 'lib/yard/handlers/processor.rb', line 160
def handles?(handler, statement) return false unless handler.matches_file?(file) if handler.method(:handles?).arity == 1 handler.handles?(statement) elsif [-1, 2].include?(handler.method(:handles?).arity) handler.handles?(statement, self) end end
#load_handlers ⇒ void
(private)
This method returns an undefined value.
Loads handlers from #handler_base_namespace. This ensures that Ruby1.9 handlers are never loaded into 1.8; also lowers the amount of modules that are loaded
# File 'lib/yard/handlers/processor.rb', line 187
def load_handlers return if @handlers_loaded[parser_type] handler_base_namespace.constants.each do |c| const = handler_base_namespace.const_get(c) unless Handlers::Base.subclasses.include?(const) Handlers::Base.subclasses << const end end @handlers_loaded[parser_type] = true end
#parse_remaining_files ⇒ void
This method returns an undefined value.
Continue parsing the remainder of the files in the globals.ordered_parser
object. After the remainder of files are parsed, processing will continue
on the current file.
#process(statements) ⇒ void
This method returns an undefined value.
Processes a list of statements by finding handlers to process each one.
# File 'lib/yard/handlers/processor.rb', line 109
def process(statements) statements.each_with_index do |stmt, _index| find_handlers(stmt).each do |handler| begin handler.new(self, stmt).process rescue HandlerAborted log.debug "#{handler} cancelled from #{caller.last}" log.debug "\tin file '#{file}':#{stmt.line}:\n\n" + stmt.show + "\n" rescue NamespaceMissingError => missingerr log.warn "The #{missingerr.object.type} #{missingerr.object.path} has not yet been recognized.\n" \ "If this class/method is part of your source tree, this will affect your documentation results.\n" \ "You can correct this issue by loading the source file for this object before `#{file}'\n" rescue Parser::UndocumentableError => undocerr log.warn "in #{handler}: Undocumentable #{undocerr.}\n" \ "\tin file '#{file}':#{stmt.line}:\n\n" + stmt.show + "\n" rescue => e log.error "Unhandled exception in #{handler}:\n" \ " in `#{file}`:#{stmt.line}:\n\n#{stmt.show}\n" log.backtrace(e) end end end end