The Wayback Machine - https://web.archive.org/web/20201231031224/https://github.com/sinatra/sinatra/issues/1667
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Route Matchers example fails #1667

Open
gscragg opened this issue Dec 9, 2020 · 1 comment
Open

Custom Route Matchers example fails #1667

gscragg opened this issue Dec 9, 2020 · 1 comment

Comments

@gscragg
Copy link

@gscragg gscragg commented Dec 9, 2020

I am trying to write my own custom route matcher and using the example in the readme, I receive the following error:
AllButPattern can't be coerced into Mustermann::Pattern (TypeError)

Here is my example code:

require 'sinatra'

get '/index' do
  'Hello world!'
end

class AllButPattern
  Match = Struct.new(:captures)

  def initialize(except)
    @except   = except
    @captures = Match.new([])
  end

  def match(str)
    @captures unless @except === str
  end
end

def all_but(pattern)
  AllButPattern.new(pattern)
end

get all_but("/index") do
  "yep yep yep"
end

It would seem as if the API for these matchers has changed with the introduction of Mustermann (4 odd years ago).

@gscragg
Copy link
Author

@gscragg gscragg commented Dec 13, 2020

I figured out a fix for my problem, but I am not sure its a good one:

require 'sinatra'

class AllButPattern

  def initialize(except)
    @except = except
  end

  def to_pattern(**options)
    return self
  end

  def params(route)
    return {} unless @except === route
  end
end

def all_but(pattern)
  AllButPattern.new(pattern)
end

get all_but("/index") do
  "yep yep yep"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.