Sha256: 93fe12365876be6fcb9eb105294183aa989d551cd9ca5a65f6b5d0eebd7022b1

Contents?: true

Size: 611 Bytes

Versions: 4

Compression:

Stored size: 611 Bytes

Contents

module Fluffle
  module Handlers
    class Dispatcher < Base
      def initialize
        @routes = []

        yield self if block_given?
      end

      # pattern - Right now just a String that 1-to-1 matches the `method`
      # block - Block to call with the `params`
      def handle(pattern, &block)
        @routes << [pattern, block]
      end

      def call(method:, params:,  **_)
        @routes.each do |(pattern, block)|
          next if pattern != method

          return block.call(*params)
        end

        raise NoMethodError, "Undefined method '#{method}'"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fluffle-0.1.0 lib/fluffle/handlers/dispatcher.rb
fluffle-0.0.3 lib/fluffle/handlers/dispatcher.rb
fluffle-0.0.2 lib/fluffle/handlers/dispatcher.rb
fluffle-0.0.1 lib/fluffle/handlers/dispatcher.rb