module Synapse::Command::MappingCommandHandler

Mixin for a command handler that uses the mapping DSL

@example

class OrderBookCommandHandler
  include MappingCommandHandler

  map_command CreateOrderbook do |command|
    # ...
  end

  map_command PlaceBuyOrder, :to => :on_buy_order
  map_command PlaceSellOrder, :to => :on_sell_order
end

Public Instance Methods

handle(command, current_unit) click to toggle source

@param [CommandMessage] command @param [UnitOfWork] current_unit Current unit of work @return [Object] The result of handling the given command

# File lib/synapse/command/mapping.rb, line 40
def handle(command, current_unit)
  mapping = command_mapper.mapping_for command.payload_type

  unless mapping
    raise ArgumentError, 'Not capable of handling [%s] commands' % command.payload_type
  end

  mapping.invoke self, command.payload, command, current_unit
end
subscribe(command_bus) click to toggle source

Subscribes this handler to the given command bus for any types that have been mapped

@param [CommandBus] command_bus @return [undefined]

# File lib/synapse/command/mapping.rb, line 54
def subscribe(command_bus)
  command_mapper.each_type do |type|
    command_bus.subscribe type, self
  end
end
unsubscribe(command_bus) click to toggle source

Unsubscribes this handler from the given command bus for any types that have been mapped

@param [CommandBus] command_bus @return [undefined]

# File lib/synapse/command/mapping.rb, line 64
def unsubscribe(command_bus)
  command_mapper.each_type do |type|
    command_bus.unsubscribe type, self
  end
end