Changeset 209

Show
Ignore:
Timestamp:
04/08/07 16:32:28 (2 years ago)
Author:
e.@brainspl.at
Message:

updated README to reflect current code base better

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/README

    r196 r209  
    1 <pre> 
     1 
    22Copyright (c) 2006 Ezra Zygmuntowicz 
    33 
    4 Merb. Mongrel+Erb 
    5  
    6 Little bitty lightweight ruby app server. For when you really need performance 
    7 for simple dynamic pages. 
     4Merb. 
     5 
     6Lightweight MVC Ruby app server. For high performance dynamic pages. 
    87 
    98** Dependencies ** 
     
    3433 
    3534*RouteMatcher and route compiler*  
     35 
    3636Reads your route definition and compiles 
    3737a method on the fly that will match the request path against each route and do the right thing. 
     
    4343Merb::RouteMatcher.prepare do |r| 
    4444  r.resources :posts 
    45   r.add '/:controller/:action/:id' 
     45  r.default_routes 
    4646  r.add '/', :controller => 'files', :action => 'index' 
    4747end 
    4848 
    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 
     49The 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* 
     61Classes with built in render method and template handling 
    12862with instance vars available in the views automatically. Merb also supports  
    12963layouts. It will look for a layout named after your controller class first and 
    13064then fall back to application.herb if no layout exists named after your controller. 
    131 You can use render_no_layout or do render :layout => :none 
     65You can use render :layout => :none. 
     66 
     67Merb does not automatically render for you in your controller actions, you have 
     68to call render yourself. I consider this a big advantage over the way rails does  
     69it for a few reasons. The main reason is that in rails you can only render once  
     70per action, so it knows if you haven’t rendered it shoudl auto render. Merb on  
     71the other hand, returns to the browser whatever the return value of your  
     72controller’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  
     74the pipe. So Merb’s render method just returns a string and needs to be the  
     75last thing you call in your action. You can render multiple times and capture  
     76the results into @ivars and then render a master template with many embeded  
     77templates. Also if you return a handle on a File or IO object from your action  
     78then merb will hand that over to mongrel to be streamed out to the client. And  
     79if you return a Proc object from your action, it will be called and the  
     80return value sent to the client. 
     81 
     82That last point has some cool connotations if you think about it. Merb does  
     83have 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  
     85though and allows for many more concurrent requests to be handled by one  
     86process. By returning a Proc object from your action, you allow merb to  
     87release the lock and the proc is called in multi threaded way. This allows  
     88for all kinds of cool streaming and ‘futures’ where you return the proc and  
     89release the mutex. It’s basically like handing over the proc to mongrel and  
     90mongrel handles calling it in a thread safe manner. 
    13291 
    13392 
     
    142101You can also render partials like so: 
    143102<%= partial(:comments) %> 
     103 
    144104This assumes a _comments.rhtml file in the same view dir as the current  
    145105controller/view 
     
    180140 
    181141 
     142*Restful Controllers* 
     143 
     144restful controllers use a different dispatch system based on the request method verbs. Merb 
     145supports multi return values based on the accept header via respond_to 
     146 
     147class Posts < Merb::Controller 
     148  # GET /posts 
     149  # GET /posts.xml 
     150  def index 
     151    @posts = Post.find :all 
     152    respond_to {|format| 
     153      format.html { render } 
     154      format.js   { render :js => 'index' } 
     155      format.xml  { render :xml => @posts.to_xml } 
     156    } 
     157  end 
     158 
     159  # GET /posts/1 
     160  # GET /posts/1.xml 
     161  def show 
     162  end 
     163 
     164  # GET /posts/new 
     165  def new 
     166  end 
     167 
     168  # GET /posts/1;edit 
     169  def edit 
     170  end 
     171 
     172  # POST /posts 
     173  # POST /posts.xml 
     174  def create 
     175  end 
     176 
     177  # PUT /posts/1 
     178  # PUT /posts/1.xml 
     179  def update 
     180  end 
     181 
     182  # DELETE /posts/1 
     183  # DELETE /posts/1.xml 
     184  def destroy 
     185  end 
     186end 
     187 
     188 
    182189*Controllers have powerful before and after filters* 
    183190 
     
    232239throw :halt, partial 'foo' 
    233240 
    234 After filters only accept a Proc and call that proc with the controller: 
     241After filters accept a symbol, string or  Proc and call that proc with the controller: 
    235242 
    236243after Proc.new {|c| Tidy.new(c.body) }, :only => :index 
    237244 
    238 Sessions are available when you start merb with the sql_session set to true or the memory_session set to true. See generated app for migration too add session table. 
    239  
    240 Helpers: dist/app/helpers/global_helper.rb will be available to all of your views. Helpers named afdter your controller plus _helper.rb will be included in the views for that controller only. 
    241  
     245Sessions are available when you start merb with the sql_session set to true or the  
     246memory_session set to true. See generated app for migration too add session table. 
     247 
     248Helpers: dist/app/helpers/global_helper.rb will be available to all of your views. 
     249 Helpers named afdter your controller plus _helper.rb will be included in the views 
     250 for that controller only. 
    242251 
    243252*The merb server* 
     
    276285end 
    277286 
     287A file upload will have a hash of params like this: 
     288{  
     289:filename => File.basename(filename),   
     290:content_type => content_type,   
     291:tempfile => body,  
     292:size => File.size(body)  
     293} 
    278294 
    279295*Merb app layout*  
     
    305321    plugins 
    306322    schema 
    307 </pre> 
  • trunk/lib/merb/mixins/controller_mixin.rb

    r208 r209  
    1818      status = input.read(boundary_size) 
    1919      raise EOFError, "bad content body"  unless status == boundary + EOL 
    20       rx = /(?:#{EOL})?#{Regexp.quote(boundary)}(#{EOL}|--)/ 
     20      rx = /(?:#{EOL})?#{Regexp.quote(boundary,'n')}(#{EOL}|--)/ 
    2121       
    2222      loop {