Sha256: e35a0afc7a22e31f32b67da90a2feb9a538b6c130fffa5b6045453255f98339c

Contents?: true

Size: 584 Bytes

Versions: 3

Compression:

Stored size: 584 Bytes

Contents

module Aggro
  # Public: Routes inter-node messages to attached message handlers.
  class MessageRouter
    def initialize
      @handlers = {}
    end

    def attach_handler(message_class, callable = nil, &block)
      if callable
        @handlers[message_class] = callable
      elsif block_given?
        @handlers[message_class] = block
      else
        fail ArgumentError
      end
    end

    def handles?(message_class)
      @handlers[message_class]
    end

    def route(message)
      @handlers[message.class].call message if handles?(message.class)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aggro-0.0.4 lib/aggro/message_router.rb
aggro-0.0.3 lib/aggro/message_router.rb
aggro-0.0.2 lib/aggro/message_router.rb