Class: Lieutenant::CommandSender

Inherits:
Object
  • Object
show all
Defined in:
lib/lieutenant/command_sender.rb

Overview

Command bus dispatch commands to the appropriate handler and manages the repository commit/clean

Instance Method Summary collapse

Constructor Details

#initialize(aggregate_repository) ⇒ CommandSender

Returns a new instance of CommandSender.



6
7
8
9
# File 'lib/lieutenant/command_sender.rb', line 6

def initialize(aggregate_repository)
  @aggregate_repository = aggregate_repository
  @handlers = {}
end

Instance Method Details

#dispatch(command) ⇒ Object Also known as: call



16
17
18
19
20
21
22
23
# File 'lib/lieutenant/command_sender.rb', line 16

def dispatch(command)
  handler = handler_for(command.class)
  # TODO: Filters
  raise(Lieutenant::Exception, "Invalid command: #{command.inspect}") unless command.valid?
  aggregate_repository.unit_of_work.execute { |repository| handler.call(repository, command) }
  # rescue Exception::ConcurrencyConflict
  #   TODO: implement command retry policy
end

#register(command_class, handler) ⇒ Object



11
12
13
14
# File 'lib/lieutenant/command_sender.rb', line 11

def register(command_class, handler)
  raise(Lieutenant::Exception, "Handler for #{command_class} already registered") if handlers.key?(command_class)
  handlers[command_class] = handler
end