Sha256: 73b782349d079faf7ea8fb7d0a3374fc9f276ef0f98fbc3286cf0c33610d2311

Contents?: true

Size: 1006 Bytes

Versions: 24

Compression:

Stored size: 1006 Bytes

Contents

module Fluffle
  module Handlers
    # Lightweight DSL for defining handler blocks for a given message
    #
    # Examples
    #
    #   dispatcher = Fluffle::Handlers::Dispatcher.new
    #   dispatcher.handle('upcase') { |str| str.upcase }
    #
    #   # Also exposed through the `Fluffle::Server#drain` method
    #   server.drain do |dispatcher|
    #     dispatcher.handle('upcase') { |str| str.upcase }
    #   end
    #
    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

24 entries across 24 versions & 1 rubygems

Version Path
fluffle-1.1.0 lib/fluffle/handlers/dispatcher.rb
fluffle-1.0.1 lib/fluffle/handlers/dispatcher.rb
fluffle-1.0.0 lib/fluffle/handlers/dispatcher.rb
fluffle-0.9.1 lib/fluffle/handlers/dispatcher.rb
fluffle-0.9.0 lib/fluffle/handlers/dispatcher.rb
fluffle-0.8.1 lib/fluffle/handlers/dispatcher.rb
fluffle-0.8.0 lib/fluffle/handlers/dispatcher.rb
fluffle-0.7.2 lib/fluffle/handlers/dispatcher.rb
fluffle-0.7.1 lib/fluffle/handlers/dispatcher.rb
fluffle-0.7.0 lib/fluffle/handlers/dispatcher.rb
fluffle-0.6.3 lib/fluffle/handlers/dispatcher.rb
fluffle-0.6.2 lib/fluffle/handlers/dispatcher.rb
fluffle-0.6.1 lib/fluffle/handlers/dispatcher.rb
fluffle-0.6.0 lib/fluffle/handlers/dispatcher.rb
fluffle-0.5.2 lib/fluffle/handlers/dispatcher.rb
fluffle-0.5.1 lib/fluffle/handlers/dispatcher.rb
fluffle-0.5.0 lib/fluffle/handlers/dispatcher.rb
fluffle-0.4.0 lib/fluffle/handlers/dispatcher.rb
fluffle-0.3.1 lib/fluffle/handlers/dispatcher.rb
fluffle-0.3.0 lib/fluffle/handlers/dispatcher.rb