Class: Brandish::Processor::Base Abstract
- Inherits:
-
Object
- Object
- Brandish::Processor::Base
- Defined in:
- lib/brandish/processor/base.rb
Overview
This class is not designed to be used and instantiated directly; this just provides common behavior for a processor. This class should be subclassed and proper behavior defined on a subclass.
A processor. This responds to a set of methods that return updated
versions of the nodes that are passed to them. Processors are all
initialized at the same time, with a context. The processor is expected
to add an object that responds to #call with arity matching 1
to the
context using Context#<<. The processor is allowed to add any number
of objects to the context using this, if need be; by default, the
processor just adds itself.
Direct Known Subclasses
Descend, Inline, Brandish::Processors::All::Comment, Brandish::Processors::All::Embed, Brandish::Processors::All::If, Brandish::Processors::All::Import, Brandish::Processors::All::Literal, Brandish::Processors::All::Verify, Brandish::Processors::Common::Asset, Brandish::Processors::Common::Group, Brandish::Processors::Common::Header, Brandish::Processors::Common::Markup, Brandish::Processors::Common::Output
Instance Attribute Summary collapse
-
#context ⇒ Context
readonly
The context associated with this processor.
Class Method Summary collapse
-
.register(map) ⇒ void
Registers processors with the global registry.
Instance Method Summary collapse
-
#accept(node) ⇒ ::Object
Accepts a node.
-
#call(node) ⇒ Parser::Node?
Processes the given node.
-
#initialize(context, options = {}) ⇒ Base
constructor
Initializes the processor with the given context.
-
#postprocess(root) ⇒ void
An optional post-process.
-
#process_block(node) ⇒ ::Object
Processes a block.
-
#process_command(node) ⇒ ::Object
Processes a command.
-
#process_root(node) ⇒ ::Object
Processes a root node.
-
#process_text(node) ⇒ ::Object
Processes a text node.
-
#setup ⇒ void
This is called by #initialize.
Constructor Details
#initialize(context, options = {}) ⇒ Base
Initializes the processor with the given context. This adds the processor to the context, and sets the context for use on the processor.
35 36 37 38 39 40 |
# File 'lib/brandish/processor/base.rb', line 35 def initialize(context, = {}) @context = context @context << self @options = setup end |
Instance Attribute Details
#context ⇒ Context (readonly)
The context associated with this processor.
27 28 29 |
# File 'lib/brandish/processor/base.rb', line 27 def context @context end |
Class Method Details
.register(map) ⇒ void
This method returns an undefined value.
Registers processors with the global registry. This interns the format and name of the processor. If the format and name pair already exists, it raises a Brandish::ProcessorError.
20 21 22 |
# File 'lib/brandish/processor/base.rb', line 20 def self.register(map) Processor.register(map) end |
Instance Method Details
#accept(node) ⇒ ::Object
Accepts a node. This passes the node through all of the processors, as well as an instance of the Descend processor.
71 72 73 |
# File 'lib/brandish/processor/base.rb', line 71 def accept(node) context.accept(node) end |
#call(node) ⇒ Parser::Node?
Processes the given node. By default, it checks the classes of the
inbound node, and maps them to process_*
blocks. If it doesn't
match, an ArgumentError
is thrown.
If this function returns a nil
value, the node should be ignored.
Context#accept acknowledges this, and skips over the remaining
processors once a processor returns a nil
value for a node.
62 63 64 65 66 67 68 |
# File 'lib/brandish/processor/base.rb', line 62 def call(node) _fix_result(_switch_node(node), node) rescue LocationError then fail rescue => e fail BuildError.new("#{e.class}: #{e.}", node.location, e.backtrace) end |
#postprocess(root) ⇒ void
This method returns an undefined value.
An optional post-process.
115 |
# File 'lib/brandish/processor/base.rb', line 115 def postprocess(root); end |
#process_block(node) ⇒ ::Object
Processes a block. By default, this performs no modifications on the node, and returns the node itself.
80 81 82 |
# File 'lib/brandish/processor/base.rb', line 80 def process_block(node) node end |
#process_command(node) ⇒ ::Object
Processes a command. By default, this performs no modifications on the node, and returns the node itself.
89 90 91 |
# File 'lib/brandish/processor/base.rb', line 89 def process_command(node) node end |
#process_root(node) ⇒ ::Object
Processes a root node. By default, this performs no modifications on the node, and returns the node itself.
98 99 100 |
# File 'lib/brandish/processor/base.rb', line 98 def process_root(node) node end |
#process_text(node) ⇒ ::Object
Processes a text node. By default, this performs no modifications on the node, and returns the node itself.
107 108 109 |
# File 'lib/brandish/processor/base.rb', line 107 def process_text(node) node end |
#setup ⇒ void
This method returns an undefined value.
This is called by #initialize. This allows subclasses to perform any nessicary setups without having to override #initialize. This does nothing by default.
47 |
# File 'lib/brandish/processor/base.rb', line 47 def setup; end |