Class: Commandos::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/commandos/dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(registry: nil) ⇒ Dispatcher

Returns a new instance of Dispatcher.



6
7
8
# File 'lib/commandos/dispatcher.rb', line 6

def initialize(registry: nil)
  @registry = registry
end

Instance Method Details

#dispatch(command) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/commandos/dispatcher.rb', line 10

def dispatch(command)
  raise RegistryNotFound if registry.nil?
  raise UnknownCommand   unless command.kind_of? IAmACommand

  registry.find_by command do |handler|
    handler = handler.new command
    result  = handler.call

    if block_given?
      yield result
    end
    
    return result
  end
end