Class: Piah::ConfigProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/piah/config_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(processor) ⇒ ConfigProcessor

Returns a new instance of ConfigProcessor.



15
16
17
# File 'lib/piah/config_processor.rb', line 15

def initialize(processor)
  @processor = processor
end

Instance Method Details

#process(config) ⇒ Object

Basic idea is that we have our list of config processors, along with the config types each one accepts. The list becomes our initial “chain”, which is a particular ordering of the processors. We then run through the chain, providing each processor with the config types it wants, and adding it’s outputs to the set of config types. The result is then a hash of all unhandled config types.

If, when processing the chain, a processor (A) outputs a config type that has already been processed by an earlier (in the chain) processor (B), we abort and rearrange the chain so that the A processor will run before the B processor, and then execute the new chain. This repeats until we get all the way through the chain without encountering processors out of order.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/piah/config_processor.rb', line 30

def process(config)
  types = @processor.types
  chain = types.keys.map{|p| [p,{}] }
  result = nil
  config = copy(config)
  until result
    result = process_chain(types,chain,copy(config))
    if result.is_a?(Set)
      # Rearrange chain
      move = chain.size.times.select do |i|
        types[chain[i][0]].to_set.intersect? result
      end
      move.each do |i|
        chain.push [chain[i][0],{}]
        chain[i] = nil
      end
      chain.compact!
      result = nil
    end
  end
  result
end