Sha256: 5495320b893dc14fc736352b580fdd940f22d59a12850cce94e53dc208e71a0a

Contents?: true

Size: 1.5 KB

Versions: 16

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true
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, except: nil, capture: nil, pattern: { }, **options)
      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

Version data entries

16 entries across 13 versions & 4 rubygems

Version Path
mustermann-1.1.2 lib/mustermann/extension.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/mustermann-1.1.1/lib/mustermann/extension.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/mustermann-1.1.1/lib/mustermann/extension.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/extension.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/extension.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.0.3/lib/mustermann/extension.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/mustermann-1.0.3/lib/mustermann/extension.rb
mustermann-1.1.1 lib/mustermann/extension.rb
mustermann-1.1.0 lib/mustermann/extension.rb
mustermann-1.0.3 lib/mustermann/extension.rb
mustermann-1.0.2 lib/mustermann/extension.rb
mustermann-1.0.2.rc2 lib/mustermann/extension.rb
mustermann-1.0.2.rc1 mustermann/lib/mustermann/extension.rb
mustermann-1.0.1 lib/mustermann/extension.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/mustermann-1.0.0/lib/mustermann/extension.rb
mustermann-1.0.0 lib/mustermann/extension.rb