-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add pattern matches to values for Mustermann::Concat. Fixes #1332 #1333
Conversation
The |
Yes, but the problem seems to be exactly that |
@dometto Could you add a test? |
Needs a test |
Sorry for the slow response. I've added a test (which indeed fails when the fix is absent). |
Thanks for adding the test. I've just confirmed that. However, I can't aggressively agree with this change. For example, one Although it seems that this change doesn't largely destroy backward compatibility (the |
Additional notes: |
Is there already an easy way to test if a |
Nope, that's not the proper way. concat = Mustermann.new("/prefix", type: :identity) + Mustermann.new('/foo')
p concat.patterns.any?{|pattern| pattern.is_a?(Mustermann::Regular) }
concat = Mustermann.new("/prefix", type: :regexp) + Mustermann.new('/foo')
p concat.patterns.any?{|pattern| pattern.is_a?(Mustermann::Regular) } |
Fair enough. 👍 I'll put together a fix based on that! |
New implementation. The check in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good with a few nits.
Thanks.
lib/sinatra/base.rb
Outdated
@@ -1024,7 +1024,9 @@ def process_route(pattern, conditions, block = nil, values = []) | |||
params.delete("ignore") # TODO: better params handling, maybe turn it into "smart" object or detect changes | |||
original, @params = @params, @params.merge(params) if params.any? | |||
|
|||
if pattern.is_a?(Mustermann::Regular) || pattern.is_a?(Mustermann::Concat) | |||
has_regexp = pattern.is_a?(Mustermann::Regular) ? true : | |||
(pattern.respond_to?(:patterns) && pattern.patterns.any? {|subpattern| subpattern.is_a?(Mustermann::Regular)} ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer AND operator over conditional operator in this case.
regexp_exists = pattern.is_a?(Mustermann::Regular) || (pattern.respond_to?(:patterns) && pattern.patterns.any? {|subpattern| subpattern.is_a?(Mustermann::Regular)} )
test/routing_test.rb
Outdated
@@ -630,17 +630,26 @@ class RoutingTest < Minitest::Test | |||
end | |||
|
|||
it 'makes regular expression captures available in params[:captures] for concatenated routes' do | |||
concatenated = Mustermann.new('/prefix') + Mustermann.new("/fo(.*)/ba(.*)", :type => :regexp) | |||
with_regexp = Mustermann.new('/prefix') + Mustermann.new("/fo(.*)/ba(.*)", :type => :regexp) | |||
without_regexp = Mustermann.new('/prefix') + Mustermann.new('/baz') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is not Mustermann::Concat
, but pure Mustermann::Sinatra
.
You should use multiple mustermann types, it will be something like:
without_regexp = Mustermann.new('/prefix', type: :identity) + Mustermann.new('/baz')
test/routing_test.rb
Outdated
@@ -630,17 +630,26 @@ class RoutingTest < Minitest::Test | |||
end | |||
|
|||
it 'makes regular expression captures available in params[:captures] for concatenated routes' do | |||
concatenated = Mustermann.new('/prefix') + Mustermann.new("/fo(.*)/ba(.*)", :type => :regexp) | |||
with_regexp = Mustermann.new('/prefix') + Mustermann.new("/fo(.*)/ba(.*)", :type => :regexp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use keyword argument instead of hash-rocket.
👍 |
Perfect! Please wait for cutting next release! |
Could you squash your commits please? 🙏 |
All ready! |
Any idea when this will be merged/released? |
Sorry for the late reply. Now I don't see any reason why we cannot merge this. Thank you for your contribution! |
This resolves #1332. Two questions:
values
? for classes other thanMustermann::Regular
andMustermann:Concat
?