Module: Synapse::Command::MappingCommandHandler

Extended by:
ActiveSupport::Concern
Includes:
CommandHandler
Defined in:
lib/synapse/command/mapping.rb

Overview

Mixin for a command handler that uses the mapping DSL

Examples:

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#handle(command, current_unit) ⇒ Object

Returns The result of handling the given command.

Parameters:

Returns:

  • (Object)

    The result of handling the given command



40
41
42
43
44
45
46
47
48
# 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) ⇒ undefined

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

Parameters:

Returns:

  • (undefined)


54
55
56
57
58
# 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) ⇒ undefined

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

Parameters:

Returns:

  • (undefined)


64
65
66
67
68
# 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