File: extension.rb

package info (click to toggle)
ruby-mustermann19 0.4.3%2Bgit20160621-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 756 kB
  • ctags: 445
  • sloc: ruby: 7,197; makefile: 3
file content (52 lines) | stat: -rw-r--r-- 1,598 bytes parent folder | download
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'sinatra/version'
fail "no need to load the Mustermann extension for #{::Sinatra::VERSION}" if ::Sinatra::VERSION >= '2.0.0'

require 'mustermann'

module Mustermann
  # Sinatra 1.x extension switching default pattern parsing over to Mustermann.
  #
  # @example With classic Sinatra application
  #   require 'sinatra'
  #   require 'mustermann'
  #
  #   register Mustermann
  #   get('/:id', capture: /\d+/) { ... }
  #
  # @example With modular Sinatra application
  #   require 'sinatra/base'
  #   require 'mustermann'
  #
  #   class MyApp < Sinatra::Base
  #     register Mustermann
  #     get('/:id', capture: /\d+/) { ... }
  #   end
  #
  # @see file:README.md#Sinatra_Integration "Sinatra Integration" in the README
  module Extension
    def compile!(verb, path, block, options = {})
      except  = options.delete(:except)
      capture = options.delete(:capture)
      pattern = options.delete(:pattern) || {}
      if path.respond_to? :to_str
        pattern[:except]  = except  if except
        pattern[:capture] = capture if capture

        if settings.respond_to? :pattern and settings.pattern?
          pattern.merge! settings.pattern do |key, local, global|
            next local unless local.is_a? Hash
            next global.merge(local) if global.is_a? Hash
            Hash.new(global).merge! local
          end
        end

        path = Mustermann.new(path, pattern)
        condition { params.merge! path.params(captures: Array(params[:captures]), offset: -1) }
      end

      super(verb, path, block, options)
    end

    private :compile!
  end
end