root/tags/0.1.0/README

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

add sleeper loop to keep db connection a live

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