soveran / syro

Simple router for web applications

Home Page:http://soveran.github.io/syro/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle Truthy / Falsey on match?

sirscriptalot opened this issue · comments

commented

I was just curious on how you would feel about changing Syro::Deck::API#match to work with truthy/falsey values?

Something like:

def match(arg)
  case arg
  when String then consume(arg)
  when Symbol then capture(arg)
  else arg
  end
end

It basically allows for routing like so:

record = Record[inbox[id]]

on record do
  res.write record.id
end

default do
  res.write 'not found'
end

I think this eliminates a gotcha with routing. For a positive check with on, you must cast your objects in some way at the moment which makes it feel a little different than Ruby's typical condition checks.

I think the problem could be that if the truthy value happens to be an instance of Symbol or String you end up with surprising effects. That's basically the reason why the comparison is very strict: to make the comparisons very explicit.

For example:

on record do
  ...
end

Just by looking at that it's not clear what the type of foo is, even if you expect it to be an instance of Record. If it turns out to be an instance of String or Symbol you will get something unexpected.

I agree that it feels different than Ruby conditionals. My hope was that by making it very strict the code would be less ambiguous. The gotcha you mention may bite you once, but what worries me is that if we change it, the other gotcha will be harder to avoid and harder to debug.

commented

Thanks for the explanation, that makes sense to me and is a fair trade off. Like you said, it probably only hits you once and that's exactly what happened to me after months of using Syro.

Thank you for bringing up this issue, it's always interesting to revisit and rethink assumptions :-)