Blocks in Routes
It's easy to register a block with a route. The block gets called before the controller and action gets invoked for that route. The block will be passed the following parameters: request, response, and cookies.
Mack::Routes.build do |r|
r.connect '/i/am/a/runner/block/that/doesnt/finish', :controller => :users, :action => :fun do |request, response, cookies|
request.params[:fun] = 'Hell Yeah!'
end
r.connect '/i/am/a/runner/block/that/finishes' do |request, response, cookies|
response.write('Hello')
throw :finished
end
end
In the first route we defined we set a parameter on the request, :fun, equal to 'Hell Yeah!'. This code block gets evaluated and the then Users#fun gets called.
In the second route we're writing 'Hello' out to the response, and then we throw :finished. When you call that throw :finished line, you're telling Mack not to bother continuing on the request stack and return the response as is. So in this case the client would receive 'Hello' as it's response.
The above routes could also be written like so:
Mack::Routes.build do |r|
r.connect '/i/am/a/runner/block/that/doesnt/finish', :controller => :tst_another, :action => :fun_runner_block, :runner_block => Proc.new({|request, response, cookies| request.params[:fun] = 'Hell Yeah!'})
r.connect '/i/am/a/runner/block/that/finishes', :runner_block => Proc.new({|request, response, cookies| response.write('Hello'); throw :finished})
end