Reputation: 22094
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.
Upvotes: 0
Views: 280
Reputation: 13739
You didn't look for the source code very well :) 10 lines below #get
method definition there is a definition of route
private method:
# lib/sinatra/base.rb, line 1212
private
def route(verb, path, options={}, &block)
# Because of self.options.host
host_name(options.delete(:host)) if options.key?(:host)
enable :empty_path_info if path == "" and empty_path_info.nil?
signature = compile!(verb, path, block, options)
(@routes[verb] ||= []) << signature
invoke_hook(:route_added, verb, path, block)
signature
end
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 (inside compile!
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 has attr_reader :routes
defined so that other classes may get acces to its @routes
instance variable).
Later when you get http request matching this route (@request
instance variable of Base
class ) the block is executed inside Base#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.
Upvotes: 2