root/tags/0.1.1/README

Revision 160, 6.9 kB (checked in by e.@brainspl.at, 2 years ago)

added the ability to freeze merb into dist/framework so you can distribute your merb app with frameowork included. generate a new app or use your old app and run rake merb:freeze . this will create the dist/framework with merb frozen into there. It will also add script/merb as the merb binary to use to run merb without gems.

  • Property svn:executable set to
Line 
1 <pre>
2 Copyright (c) 2006 Ezra Zygmuntowicz
3
4 Merb. Mongrel+Erb
5
6 Little bitty lightweight ruby app server. For when you really need performance
7 for simple dynamic pages.
8
9 **Sample APP included**
10
11 ** Dependencies **
12 mongrel
13 erubis
14 json or fjson
15 mime-types
16 archive-tar-minitar
17 rspec
18
19 Install these gems first then you can build the merb gem from svn trunk like so:
20 $ sudo gem install mongrel erubis json mime-types archive-tar-minitar rspec --include-dependencies
21 $ svn co http://svn.devjavu.com/merb/trunk merb
22 $ cd merb
23 $ rake install
24
25 To generate a new merb app after the gem is installed:
26 $ merb -g myapp
27
28
29 **FEATURES**
30
31 *Mongrel handler*
32 built in that parses incoming requests
33 including multipart uploads and post as well as ?query=strings. Puts the
34 params into params and the cookies into cookies when it instantiates your
35 controller class.
36
37 *RouteMatcher and route compiler*
38 Reads your route definition and compiles
39 a method on the fly that will match the request path against each route and do the
40 right thing. So the following routes:
41
42 Merb::RouteMatcher.prepare do |r|
43   r.add '/foo/:bar/baz/:id', :controller => 'Test', :action => 'foo'
44   r.add '/:controller/:action/:id'
45   r.add '/bar/:*rest', :controller => 'Test', :action => 'glob'
46 end
47
48 Will be compiled and defined as a method with this lambda as the body:
49
50 lambda{|path|
51   case path
52   when Regexp.new('/foo/([^/;.,?]+)/baz/([^/;.,?]+)')
53     @sections[:bar] = $1
54     @sections[:id] = $2
55     return {:controller=>"Test", :action=>"foo"}.merge(@sections)
56   when /\A\/([^\/;.,?]+)(?:\/?\Z|\/([^\/;.,?]+)\/?)(?:\/?\Z|\/([^\/;.,?]+)\/?)\Z/
57       @sections[:controller] = $1
58       @sections[:action] = $2 || 'index'
59       @sections[:id] = $3 || nil
60       return @sections
61   when Regexp.new('/bar/([^;.,?]+)')
62     @sections[:rest] = $1
63     return {:controller=>"Test", :action=>"glob"}.merge(@sections)
64   else
65     return {:controller=>'Noroutefound', :action=>'noroute'}
66   end
67 }
68
69 *Simple Controllers*
70 classes with built in render method and template handling
71 with instance vars available in the views automatically. Merb also supports
72 layouts. It will look for a layout named after your controller class first and
73 then fall back to application.herb if no layout exists named after your controller.
74 You can use render_no_layout or do render :layout => :none
75
76
77 class Test < Merb::Controller
78   def hello
79     # params, headers and cookies are available here.
80     @name = params[:name]
81     render
82   end
83 end
84
85 You can also render partials like so:
86 <%= partial(:comments) %>
87 This assumes a _comments.rhtml file in the same view dir as the current
88 controller/view
89
90 Partials compile the template ands returns a string. So you can also call
91 them and assign them to a var if you want:
92
93 def someaction
94   @one = partial(:one)
95   @two = partial(:two)
96 end
97
98 partials can also render views from other controllers by specifying the path
99
100 partial('/shared/foo')
101
102
103 Merb also allows for returning javascript instead of html for ajax actions
104 You have to use the render_js instead of normal render
105
106 def ajax_action
107   @posts = Post.find :all
108   render_js
109 end
110
111 # ajax_action.jerb
112 $('comments').update('<%=js partial(:posts) %>');
113
114 # _posts.herb
115 <ul>
116   <% @posts.each do |p| %>
117     <li>
118     <%= p.title %><br />
119     <%= p.body %>
120     </li>
121   <% end %>
122 </ul>   
123
124
125 *Controllers have powerful before and after filters*
126
127 Use the before method in your controllers. before accepts either a symbol
128 or a Proc/lambda object. If you give it a symbol it will call a method with
129 the same name as the symbol. If you give it a proc that takes one argument
130 it will call the proc with the current controller as that argument. You can
131 use :only and :exclude as options to your filters to exclude or include actionsfrom certain filters. :only and :exclude take :symbols or [:sym, :sam]
132 array of symbols.
133
134 class Foo < Merb::Controller
135  
136   before :setup_user, :only => :foo
137   before lambda {|c| c.headers['X-Foo] = 'bar' }, :exclude => [:foo, :baz]
138
139   def setup_user
140     # blah blah
141   end
142
143   def foo
144     # blah
145   end
146
147   def regular_action
148     # blah
149   end
150
151 end
152
153 To stop the before filter chain you use throw :halt with a few options:
154
155 # halts the filter chain and calls filters_halted which you can override
156 # in your controller to specialize it.
157
158 throw :halt
159
160 # halts the filters and calls the method named after the symbol:
161
162 throw :halt, :other_action
163
164 # halts the filter chain and returns the result of the Proc being called
165
166 throw :halt, Proc.new{ |c| c.redirect "/foo" }
167
168 # halts the chain and returns whatever is in the string
169
170 throw :halt, "<h1>You don't have permissions IDIOT!</h1>"
171
172 or even render templates:
173
174 throw :halt, render 'foo'
175 throw :halt, partial 'foo'
176
177 After filters only accept a Proc and call that proc with the controller:
178
179 after Proc.new {|c| Tidy.new(c.body) }, :only => :index
180
181 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.
182
183 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.
184
185
186 *The merb server*
187 right now you add your routes in
188 the appdir/dist/conf/router.rb file. So by default it runs on port 4000
189
190 $ cd /path/to/your/merb/app
191 $ merb 
192
193 Or to start merb on a different port:
194 $ merb -p 3500
195
196 To start a cluster of merb servers you specify the first port and then how many
197 servers you want spawned. SO this command will start a merb instance on ports
198 3000, 3001, 3002
199
200 $ merb -p 3000 -c 3
201
202 To start a Merb IRB console where all your models and other classes are pre loaded
203 use the -i flag
204
205 $merb -i
206
207 *File uploads*
208 This is one of the things that Merb was written for. Rails doesn't allow
209 multiple concurrent file uploads at once without blocking an entire rails backend for each file upload. Merb allows multiple file uploads at once.
210 progress bar. When a file is uploaded with Merb, it gets put in a Tempfile. So
211 you just want to copy it to the right place on the filesystem.
212
213 def upload
214   puts params[:file].inspect
215  
216   FileUtils.mv params[:file][:tempfile].path, MERB_ROOT+"/uploads/#{params[:file][:filename]}"
217
218   render
219 end
220
221
222 *Merb app layout*
223
224 A Merb app contains everything it needs to run in production in the
225 MERB_ROOT/dist directory. So for deployment you only need to deploy the dist dir. This
226 keeps your test code and development plugins separate from your main app and lets you
227 not deploy them to the live server. You deal with two things with this setup, MERB_ROOT
228 and DIST_ROOT. MERB_ROOT is the root of the whole tree. And DISTROOT is MERB_ROOT+/dist
229 You will cd into MERB_ROOT to run the merb command line. ANd when you deploy live you
230 will put the dist dir into another empty MERB_ROOT on the production server.
231
232 merb_app:
233   Rakefile
234   README
235   scripts
236   test
237     spec
238     unit
239   plugins
240   dist
241     app
242       controllers
243       models
244       views
245     conf
246     lib
247     public
248     plugins
249     schema
250 </pre>
Note: See TracBrowser for help on using the browser.