mustermann
mustermann copied to clipboard
Anchors
If I dont set anchors and the option to ignore checking for them, is mustermann automatically putting them in at the beginning and end of my route like in sinatra i do:
get %r{/stuff/([0-9a-f]{32})}i
is that the same as
get %r{\A/stuff/([0-9a-f]{32})\z}i, :mustermann_opts => { :type => :regexp, :check_anchors => false}
How do I know where mustermann is applying the anchors without specifically setting them?
@rschwass Could you write up a spec for reproducing your paint point?
First run of an old app under sinatra v2, exciting! Oh, an error…
web.1 | ! Unable to load application: Mustermann::CompileError: regular expression should not contain \A: "\\A\\/(\\d+)\\/?\\Z"
I find I've wrapped a lot of path patterns in anchors.
$ ag '\\A'
app/api.rb
127: options %r{\A/(\d+)/?\Z} do |_|
182: get %r{\A/(\d+)/?\Z}, :provides => :json do |id|
191: put %r{\A/(\d+)/?\Z}, :provides => :json do |id| # update
210: delete %r{\A/(\d+)/?\Z}, :provides => :json do |id|
220: options %r{\A/(\d+)/tags/?\Z} do |_|
227: get %r{\A/(\d+)/tags/?\Z}, :provides => :json do |id|
234: patch %r{\A/(\d+)/tags/?\Z}, :provides => :json do |id|
259: delete %r{\A/(\d+)/tags/?\Z}, :provides => :json do |id|
I update all of the above to use ^
and $
# run app again
web.1 | ! Unable to load application: Mustermann::CompileError: regular expression should not contain ^: "^\\/(\\d+)\\/?$"
Using anchors is good practice with regex but Mustermann is new to me. If it does deal with it then I'm fine, it's just good to know or not.
Regards, iain
Mustermann will insert them for you. The reason it insists on not having them in there is to allow pattern concatenation without having to rewrite the regular expression (which might be faulty).
@rkh Okay, that makes sense. Thanks.
Mustermann will insert them for you. The reason it insists on not having them in there is to allow pattern concatenation without having to rewrite the regular expression (which might be faulty).
That is a really good point I never thought of before, perhaps this should go in the docs if it's not already!