When you define a route, the key of each member of the options hash is called as a method, with the value passed as arguments .
So, when you do get '/', :auth => :user do ... , the auth method is called with the argument :user . This in turn calls the condition method with a block.
The condition method is actually the one above, where you refer to which is its use. It looks like this:
def condition(name = "#{caller.first[/`.*'/]} condition", &block) @conditions << generate_method(name, &block) end
The generate_method method converts the block into a method with the given name, and then this method is stored in the @conditions array, the contents of @conditions then saved with the route definition, and @conditions is cleared for the next route definition.
At this point, the code block passed to condition was not executed. In fact, it was saved later.
When a real request arrives, if the request path matches the route, then each condition associated with this route is fulfilled to check if it is satisfied. In this example, is it when redirect '/login' unless user_logged_in? runs first, so session will be configured and session[:user] will be available (or not if they are not logged in).
It is important to understand that when you pass a block to a method, the code in that block does not have to be called right away. In this case, the code in the block is called only upon receipt of the actual request.
source share