Sha256: 5905daab89c2d7889103ad121df8e275ff501fd2b7bcfe5d263ffbb5576fc2f6

Contents?: true

Size: 720 Bytes

Versions: 2

Compression:

Stored size: 720 Bytes

Contents

module Beryl
  module Routing
    module Matcher
      extend self

      DEFAULT_CONSTRAINT = '.*'

      def match(route, path)
        params = params(route)
        r = params.each_with_object("#{route.clone}") do |param, result|
          result.sub!(":#{param}", "(#{DEFAULT_CONSTRAINT})")
        end
        regex = /\A#{r}\z/
        matched = regex.match(path)
        return false unless matched
        params(route).each_with_object({}).with_index do |(param, result), index|
          result[param] = matched[index + 1]
        end

      end

      private

      def params(route)
        route.scan(/:[[:lower:]_]+[[:lower:][:digit:]_]*/).map { |param| param[1..-1].to_sym }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
beryl-0.3.1 lib/beryl/routing/matcher.rb
beryl-0.3.0 lib/beryl/routing/matcher.rb