Class: Brandish::Processor::Context
- Inherits:
-
Object
- Object
- Brandish::Processor::Context
- Extended by:
- Forwardable
- Defined in:
- lib/brandish/processor/context.rb
Overview
The state associated with all of the processors. Since all processors are independant class instances, instance variables that are used in one processor does not affect another; however, shared state can be used through the context.
The context also keeps track of the processors being used for the processor, and distributes the processing management throughout all of the processors.
Defined Under Namespace
Classes: Skip
Instance Attribute Summary collapse
-
#configure ⇒ Configure
readonly
The configuration for the build.
-
#form ⇒ Configure::Form
readonly
The form that is being processed.
-
#processors ⇒ <#call>
readonly
The processors that are going to be run on an accept.
Instance Method Summary collapse
-
#<<(*processors) ⇒ <#call>
Adds processors to the processor list.
-
#[](key) ⇒ ::Object
Sets a key on the context.
-
#[]=(key, value) ⇒ ::Object
Sets a key on the context.
-
#accept(node) ⇒ ::Object
Accepts a node.
-
#fetch(key) ⇒ ::Object
Fetches a value at the given key, or provides a default if the key doesn't exist.
-
#initialize(configure, form) ⇒ Context
constructor
Initialize the context, to set up the internal state.
-
#merge(options) ⇒ void
Merges the given options into this context.
-
#process(root) ⇒ ::Object
Performs the processing of the given root node.
-
#push(*processors) ⇒ <#call>
Adds processors to the processor list.
-
#unshift(*processors) ⇒ <#call>
Adds a processor to the start of the processor list.
Constructor Details
#initialize(configure, form) ⇒ Context
Initialize the context, to set up the internal state.
114 115 116 117 118 119 120 121 |
# File 'lib/brandish/processor/context.rb', line 114 def initialize(configure, form) @processors = [] @configure = configure @form = form @descent = Processor::Descend.new(self) @buffer = [] @options = {} end |
Instance Attribute Details
#configure ⇒ Configure (readonly)
The configuration for the build. This is used for output directories and the like.
106 107 108 |
# File 'lib/brandish/processor/context.rb', line 106 def configure @configure end |
#form ⇒ Configure::Form (readonly)
The form that is being processed.
111 112 113 |
# File 'lib/brandish/processor/context.rb', line 111 def form @form end |
#processors ⇒ <#call> (readonly)
The processors that are going to be run on an accept. This can be
a Base subclass, or any object that responds to #call
.
100 101 102 |
# File 'lib/brandish/processor/context.rb', line 100 def processors @processors end |
Instance Method Details
#<<(*processors) ⇒ <#call>
Adds processors to the processor list. This delegates to #processors.
41 |
# File 'lib/brandish/processor/context.rb', line 41 delegate [:<<, :push, :unshift] => :@processors |
#[](key) ⇒ ::Object
Sets a key on the context. This gets an option that is used for all processors on this context.
87 |
# File 'lib/brandish/processor/context.rb', line 87 delegate [:[], :[]=, :fetch] => :@options |
#[]=(key, value) ⇒ ::Object
Sets a key on the context. This sets an option that is used for all processors on this context.
87 |
# File 'lib/brandish/processor/context.rb', line 87 delegate [:[], :[]=, :fetch] => :@options |
#accept(node) ⇒ ::Object
Accepts a node. This passes the node through all of the processors, as well as an instance of the Descend processor.
138 139 140 141 142 143 144 |
# File 'lib/brandish/processor/context.rb', line 138 def accept(node) # Injects the node over all effective processors. Every iteration will # use the value returned by the last `process` method call, unless it # is `nil`. result = effective_processors.inject(node) { |n, p| accept_node_with(n, p) } result.is_a?(Skip) ? result.node : result end |
#fetch(key) ⇒ ::Object
Fetches a value at the given key, or provides a default if the key doesn't exist. If both a block and a default argument are given, the block form takes precedence.
87 |
# File 'lib/brandish/processor/context.rb', line 87 delegate [:[], :[]=, :fetch] => :@options |
#merge(options) ⇒ void
This method returns an undefined value.
Merges the given options into this context.
94 |
# File 'lib/brandish/processor/context.rb', line 94 def_delegator :@options, :merge!, :merge |
#process(root) ⇒ ::Object
Performs the processing of the given root node. This should be a Brandish::Parser::Node::Root.
128 129 130 131 |
# File 'lib/brandish/processor/context.rb', line 128 def process(root) root = accept(root) effective_processors.each { |p| p.postprocess(root) } end |
#push(*processors) ⇒ <#call>
Adds processors to the processor list. This delegates to #processors.
41 |
# File 'lib/brandish/processor/context.rb', line 41 delegate [:<<, :push, :unshift] => :@processors |
#unshift(*processors) ⇒ <#call>
Adds a processor to the start of the processor list. This delegates to #processors.
41 |
# File 'lib/brandish/processor/context.rb', line 41 delegate [:<<, :push, :unshift] => :@processors |