summaryrefslogtreecommitdiff
path: root/bocelli/core/router.rb
blob: 16b43716f2de1c9c2f6084ea23a7fe11c43819ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Bocelli
  module Core
    module Router
      def setup_router
        @routes = {}
      end

      def on(route, &block)
        raise 'no block given' if block.nil?

        @routes[route] = block
      end

      def match?(str, route)
        case route
        when Regexp
          str =~ route
        when String
          str == route
        end
      end

      def match(str)
        @routes.detect { |k, _| match?(str, k) }
      end
    end
  end
end