Class: Leonidas::Commands::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/leonidas/commands/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(handlers) ⇒ Processor

Returns a new instance of Processor.



6
7
8
9
10
11
12
# File 'lib/leonidas/commands/processor.rb', line 6

def initialize(handlers)
  @handlers = [ ]
  handlers.each do |handler|
    raise TypeError, "Argument must include Leonidas::Commands::Handler" unless handler.class < ::Leonidas::Commands::Handler
    @handlers << handler
  end
end

Instance Method Details

#process(commands, persist = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/leonidas/commands/processor.rb', line 14

def process(commands, persist=false)
  commands.sort! {|command1, command2| command1.timestamp <=> command2.timestamp}
  commands.each do |command|
    raise TypeError, "Argument must be a Leonidas::Commands::Command" unless command.is_a? ::Leonidas::Commands::Command
    @handlers.each do |command_handler|
      if command_handler.handles? command
        command_handler.run(command) 
        command_handler.persist(command) if persist
      end
    end
  end
end