ruby - How does this custom route matcher in Sinatra example work? -
in sinatra readme, there section called custom route matchers following example:
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 all_but("/index") # ... end would helpful enough talk me through how works? bit i'm not sure why example has match struct , captures are. user can't set @captures instance variable, @except one; how captures used?
when route processed, takes argument get (or post or whatever), , sends object's match method path argument. expects either nil mean didn't match, or array of captures. object string or regex , both have match method.
sinatra calls on captures method of object when processing route. example uses struct easy way set , respond object respond captures, , puts in array, that's captures return. it's empty because string hasn't been checked captures, it's negative filter. such, i'd prefer the use of before filter this, finding clear , useful examples difficult.
Comments
Post a Comment