File: composite_sexp_processor.rb

package info (click to toggle)
ruby-sexp-processor 4.17.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 380 kB
  • sloc: ruby: 5,619; makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,144 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require "sexp_processor"

##
# Implements the Composite pattern on SexpProcessor. Need we say more?
#
# Yeah... probably. Implements a SexpProcessor of SexpProcessors so
# you can easily chain multiple to each other. At some stage we plan
# on having all of them run +process+ and but only ever output
# something when +generate+ is called, allowing for deferred final
# processing.

class CompositeSexpProcessor < SexpProcessor

  ##
  # The list o' processors to run.

  attr_reader :processors

  def initialize # :nodoc:
    super
    @processors = []
  end

  ##
  # Add a +processor+ to the list of processors to run.

  def << processor
    raise ArgumentError, "Can only add sexp processors" unless
      SexpProcessor === processor || processor.respond_to?(:process)
    @processors << processor
  end

  ##
  # Run +exp+ through all of the processors, returning the final
  # result.

  def process exp
    @processors.each do |processor|
      exp = processor.process(exp)
    end
    exp
  end

  def on_error_in node_type, &block
    @processors.each do |processor|
      processor.on_error_in(node_type, &block)
    end
  end
end