I want figure out the connection between the Rack and Sinatra, so I dig into the source code, then I found the the definition of the basic class method get:
def get(path, opts={}, &block)
conditions = @conditions.dup
route('GET', path, opts, &block)
@conditions = conditions
route('HEAD', path, opts, &block)
end
now what’s the method: route? I’m currently using yard document tool, I just can’t find the definition of route in any Sinatra code or even Rack code.
You didn’t look for the source code very well 🙂 10 lines below
#getmethod definition there is a definition ofrouteprivate method:This is a private method and you will not find it in the Sinatra documentation.
Generally this method does the following: It create proc from passed
&block, combine it with http path, keys and invocation conditions (insidecompile!method) and store it in@routes[verb]class instance variable so that block may be found by path and conditions and executed later on (this class also hasattr_reader :routesdefined so that other classes may get acces to its@routesinstance variable).Later when you get http request matching this route (
@requestinstance variable ofBaseclass ) the block is executed insideBase#route!method (see line 795).I would recommend you to use some IDE that help to examin source code. For example I use Rubymine for this purpose and its feature
Go To -> Declaration: Just put your cursor on the variable/method/class/etc, press F12 and Rubymine will find it for you, even in source code of connected gems.