Class: Brandish::Processor::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/brandish/processor/base.rb

Overview

This class is abstract.

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • context (Context)
  • options (::Object) (defaults to: {})

    The options for this processor.



35
36
37
38
39
40
# File 'lib/brandish/processor/base.rb', line 35

def initialize(context, options = {})
  @context = context
  @context << self
  @options = options
  setup
end

Instance Attribute Details

#contextContext (readonly)

The context associated with this processor.

Returns:



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.

Examples:

Processor.register [:html, :stripper] => self
Processor.register [:all, :descend] => self

Parameters:

  • map ({(::Symbol, ::Symbol) => Processor::Base})

    The processors to register.

Raises:

  • (ProcessorError)

    If one of the format and name pairs already exists.



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.

Parameters:

Returns:

  • (::Object)


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.

Parameters:

Returns:

Raises:



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.message}", node.location,
    e.backtrace)
end

#postprocess(root) ⇒ void

This method returns an undefined value.

An optional post-process.

Parameters:



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.

Parameters:

Returns:

  • (::Object)


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.

Parameters:

Returns:

  • (::Object)


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.

Parameters:

Returns:

  • (::Object)


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.

Parameters:

Returns:

  • (::Object)


107
108
109
# File 'lib/brandish/processor/base.rb', line 107

def process_text(node)
  node
end

#setupvoid

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