Sha256: 5a97a6101fe608fe8e511443ba52c464fb615cccab71c24599e7555b5f2ed811

Contents?: true

Size: 604 Bytes

Versions: 2

Compression:

Stored size: 604 Bytes

Contents

module Commandos
  UnknownCommand   = Class.new StandardError
  RegistryNotFound = Class.new StandardError

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

    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

  private
    attr_reader :registry
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
commandos-0.1.5 lib/commandos/dispatcher.rb
commandos-0.1.4 lib/commandos/dispatcher.rb