summaryrefslogtreecommitdiff
path: root/bocelli/module/base.rb
blob: 45b09d79bcc19acd587576568ff196c7837d201c (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
29
30
31
32
33
34
35
36
37
38
module Bocelli
  module Module
    module Base
      attr_reader :routes

      def setup
        @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

      class << self
        def extended(mod)
          super

          mod.setup
        end
      end
    end
  end
end