Class: Brandish::Processor::Context

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#configureConfigure (readonly)

The configuration for the build. This is used for output directories and the like.

Returns:



106
107
108
# File 'lib/brandish/processor/context.rb', line 106

def configure
  @configure
end

#formConfigure::Form (readonly)

The form that is being processed.

Returns:



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.

Returns:

  • (<#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.

Returns:

  • (<#call>)


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.

Parameters:

  • key (::Symbol, ::String)

    The key.

Returns:

  • (::Object)


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.

Parameters:

  • key (::Symbol, ::String)

    The key.

  • value (::Object)

    The value.

Returns:

  • (::Object)


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.

Parameters:

Returns:

  • (::Object)


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.

Attempts to retrieve a value at the given key. If there is no key-value pair at the given key, it raises an error.

Parameters:

  • key (::Symbol, ::String)

    The key.

Returns:

  • (::Object)

    The value.

Raises:

  • (KeyError)

    if the key isn't on the context.



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.

Parameters:

  • options ({::Symbol, ::String => ::Object})


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.

Parameters:

Returns:

  • (::Object)


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.

Returns:

  • (<#call>)


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.

Returns:

  • (<#call>)


41
# File 'lib/brandish/processor/context.rb', line 41

delegate [:<<, :push, :unshift] => :@processors