| 49 | | Will be compiled and defined as a method with this lambda as the body: |
|---|
| 50 | | |
|---|
| 51 | | lambda{|path| |
|---|
| 52 | | case path |
|---|
| 53 | | when Regexp.new('/posts/([^/,?]+)[;]edit') |
|---|
| 54 | | @sections[:id] = $1 |
|---|
| 55 | | return {:rest=>true, :controller=>"posts", :allowed=>{:get=>"edit"}}.update(@sections) |
|---|
| 56 | | when Regexp.new('/posts/new[;]([^/,?]+)') |
|---|
| 57 | | @sections[:action] = $1 |
|---|
| 58 | | return {:rest=>true, :controller=>"posts", :allowed=>{:post=>"new", :get=>"new", :delete=>"new", :put=>"new"}}.update(@sections) |
|---|
| 59 | | when Regexp.new('/posts/new') |
|---|
| 60 | | return {:rest=>true, :controller=>"posts", :allowed=>{:get=>"new"}}.update(@sections) |
|---|
| 61 | | when Regexp.new('/posts/([^/,?]+)\.([^/,?]+)') |
|---|
| 62 | | @sections[:id] = $1 |
|---|
| 63 | | @sections[:format] = $2 |
|---|
| 64 | | return {:rest=>true, :controller=>"posts", :allowed=>{:get=>"show", :delete=>"destroy", :put=>"update"}}.update(@sections) |
|---|
| 65 | | when Regexp.new('/posts\.([^/,?]+)') |
|---|
| 66 | | @sections[:format] = $1 |
|---|
| 67 | | return {:rest=>true, :controller=>"posts", :allowed=>{:post=>"create", :get=>"index"}}.update(@sections) |
|---|
| 68 | | when Regexp.new('/posts/([^/,?]+)') |
|---|
| 69 | | @sections[:id] = $1 |
|---|
| 70 | | return {:rest=>true, :controller=>"posts", :allowed=>{:get=>"show", :delete=>"destroy", :put=>"update"}}.update(@sections) |
|---|
| 71 | | when Regexp.new('/posts/?') |
|---|
| 72 | | return {:rest=>true, :controller=>"posts", :allowed=>{:post=>"create", :get=>"index"}}.update(@sections) |
|---|
| 73 | | when /\A\/([^\/;.,?]+)(?:\/?\Z|\/([^\/;.,?]+)\/?)(?:\/?\Z|\/([^\/;.,?]+)\/?)\Z/ |
|---|
| 74 | | @sections[:controller] = $1 |
|---|
| 75 | | @sections[:action] = $2 || 'index' |
|---|
| 76 | | @sections[:id] = $3 if $3 |
|---|
| 77 | | return @sections |
|---|
| 78 | | when Regexp.new('/') |
|---|
| 79 | | return {:controller=>"files", :action=>"index"}.update(@sections) |
|---|
| 80 | | else |
|---|
| 81 | | return {:controller=>'Noroutefound', :action=>'noroute'} |
|---|
| 82 | | end |
|---|
| 83 | | } |
|---|
| 84 | | |
|---|
| 85 | | |
|---|
| 86 | | *Restful Controller* |
|---|
| 87 | | |
|---|
| 88 | | restful controllers use a different dispatch system based on the request method verbs. |
|---|
| 89 | | |
|---|
| 90 | | class Posts < Merb::Controller |
|---|
| 91 | | # GET /posts |
|---|
| 92 | | # GET /posts.xml |
|---|
| 93 | | def index |
|---|
| 94 | | end |
|---|
| 95 | | |
|---|
| 96 | | # GET /posts/1 |
|---|
| 97 | | # GET /posts/1.xml |
|---|
| 98 | | def show |
|---|
| 99 | | end |
|---|
| 100 | | |
|---|
| 101 | | # GET /posts/new |
|---|
| 102 | | def new |
|---|
| 103 | | end |
|---|
| 104 | | |
|---|
| 105 | | # GET /posts/1;edit |
|---|
| 106 | | def edit |
|---|
| 107 | | end |
|---|
| 108 | | |
|---|
| 109 | | # POST /posts |
|---|
| 110 | | # POST /posts.xml |
|---|
| 111 | | def create |
|---|
| 112 | | end |
|---|
| 113 | | |
|---|
| 114 | | # PUT /posts/1 |
|---|
| 115 | | # PUT /posts/1.xml |
|---|
| 116 | | def update |
|---|
| 117 | | end |
|---|
| 118 | | |
|---|
| 119 | | # DELETE /posts/1 |
|---|
| 120 | | # DELETE /posts/1.xml |
|---|
| 121 | | def destroy |
|---|
| 122 | | end |
|---|
| 123 | | end |
|---|
| 124 | | |
|---|
| 125 | | |
|---|
| 126 | | *Simple Controllers* |
|---|
| 127 | | classes with built in render method and template handling |
|---|
| | 49 | The r.default_routes routes adds the standard routes: |
|---|
| | 50 | |
|---|
| | 51 | /controller/action/id.xml |
|---|
| | 52 | /controller/action/id |
|---|
| | 53 | /controller/action.xml |
|---|
| | 54 | /controller/action |
|---|
| | 55 | /controller.xml # index action |
|---|
| | 56 | /controller # index action |
|---|
| | 57 | |
|---|
| | 58 | |
|---|
| | 59 | |
|---|
| | 60 | *Controllers* |
|---|
| | 61 | Classes with built in render method and template handling |
|---|
| 131 | | You can use render_no_layout or do render :layout => :none |
|---|
| | 65 | You can use render :layout => :none. |
|---|
| | 66 | |
|---|
| | 67 | Merb does not automatically render for you in your controller actions, you have |
|---|
| | 68 | to call render yourself. I consider this a big advantage over the way rails does |
|---|
| | 69 | it for a few reasons. The main reason is that in rails you can only render once |
|---|
| | 70 | per action, so it knows if you havenât rendered it shoudl auto render. Merb on |
|---|
| | 71 | the other hand, returns to the browser whatever the return value of your |
|---|
| | 72 | controllerâs action method is. This opens up more possibilities imho because |
|---|
| | 73 | now you can return any string from your action and that will be sent down |
|---|
| | 74 | the pipe. So Merbâs render method just returns a string and needs to be the |
|---|
| | 75 | last thing you call in your action. You can render multiple times and capture |
|---|
| | 76 | the results into @ivars and then render a master template with many embeded |
|---|
| | 77 | templates. Also if you return a handle on a File or IO object from your action |
|---|
| | 78 | then merb will hand that over to mongrel to be streamed out to the client. And |
|---|
| | 79 | if you return a Proc object from your action, it will be called and the |
|---|
| | 80 | return value sent to the client. |
|---|
| | 81 | |
|---|
| | 82 | That last point has some cool connotations if you think about it. Merb does |
|---|
| | 83 | have a mutex lock around the call to your controllerâs action anywhere that |
|---|
| | 84 | you can call AR objects. Merbâs lock is way smaller then rails giant lock |
|---|
| | 85 | though and allows for many more concurrent requests to be handled by one |
|---|
| | 86 | process. By returning a Proc object from your action, you allow merb to |
|---|
| | 87 | release the lock and the proc is called in multi threaded way. This allows |
|---|
| | 88 | for all kinds of cool streaming and âfuturesâ where you return the proc and |
|---|
| | 89 | release the mutex. Itâs basically like handing over the proc to mongrel and |
|---|
| | 90 | mongrel handles calling it in a thread safe manner. |
|---|