root/tags/0.5.2/README

Revision 1228, 9.5 kB (checked in by e.@brainspl.at, 11 months ago)

checking in server.rb refactor, split into config.rb boot_loader.rb, integrated Merb.root stuff. This will probably break a few small things and maybe some plugins but bear with me as this is a needed change. do not use Merb::Server.config[:foo] anymore, instead use Merb::Config[:foo], do not use MERB_ROOT anymore use Merb.root instead

  • Property svn:executable set to
Line 
1 = Merb
2
3 Like Ruby on Rails, Merb (Mongrel + ERB) is an MVC framework. Unlike Rails, Merb is ORM-agnostic, JavaScript library agnostic, and template language agnostic, preferring plugins that add in support for a particular feature rather than trying to produce a monolithic library with everything in the core. In fact, this is a guiding principle of the project, which has led to third-party support for the ActiveRecord, DataMapper, and Sequel ORMs.
4
5 In addition, it means that the core code in Merb is kept simple and well organised. This has multiple benefits. It means it's faster for one thing. It's also easier to understand, maintain and extend.
6
7 === Get Merb
8
9 The simplest way to get Merb is to install the gem:
10
11   $ sudo gem install merb --include-dependencies
12  
13 If you want to contribute (or just use the latest code), you can build the gem from the svn trunk:
14
15   $ sudo gem install mongrel json json_pure erubis mime-types rspec hpricot mocha rubigen haml markaby mailfactory Ruby2Ruby -y
16   $ svn co http://svn.devjavu.com/merb/trunk merb
17   $ cd merb
18   $ rake install
19
20 To generate a new merb app after the gem is installed:
21  
22   $ merb myapp
23
24 ==== Dependencies
25
26 Currently, Merb itself depends on the following gems:
27
28 * mongrel
29 * json_pure
30 * erubis
31 * mime-types
32 * rspec
33 * rubigen
34 * ruby2ruby
35 * rake
36  
37 ** If you are on windows see this blog post oj how to get up and running:
38 http://www.ghostonthird.com/2007/11/17/merb-on-windows-it-works/
39  
40 You must also have either the json or json_pure gem installed. Note that the json gem
41 provides a faster library but will not work with jRuby.
42  
43 Optionally, merb can take advantage of the following gems:
44  
45 * mailfactory (if you wish to use merb's mailers)
46 * haml 1.8 or greater (if you wish to use HAML templates, i.e. .haml files)
47 * markaby (if you wish to use Markaby template, i.e. .mab files)
48 * builder (if you wish to use Builder templates, i.e. .rxml, .rerb or .builder files)
49 * memcache-client (for use with Danga Interactive's memcached)
50 * swiftiply
51 * eventmachine
52 * rcov
53 * ruby-debug (if you want to use debugging functionality)
54
55 You will also probably need to install your ORM of choice as well as any gem plugins you want to use (see below).
56
57 === The +merb+ server
58
59 right now you add your routes in
60 the appdir/config/router.rb file. So by default it runs on port 4000
61
62         $ cd /path/to/your/merb/app
63         $ merb 
64
65 Or to start merb on a different port:
66
67         $ merb -p 3500
68
69 To start a cluster of merb servers you specify the first port and then how many
70 servers you want spawned. SO this command will start a merb instance on ports
71 3000, 3001, 3002
72
73         $ merb -p 3000 -c 3
74
75 To start a Merb IRB console where all your models and other classes are pre loaded
76 use the -i flag
77
78         $ merb -i
79
80 To enable ruby-debug support, start Merb with
81
82   $ merb -D
83
84 Now you can use the +debugger+ method everywhere in your code to open an rdebug session.
85        
86 To see all the available command line flags use:
87
88         $ merb -h
89                
90        
91 == Using Merb
92
93 Merb uses the Model-View-Controller (MVC) pattern.  Incoming requests are matched in the Router and
94 directed to an appropriate controller action.
95
96 === Model
97
98 Merb does not come with its own model layer.  While you are free to use whatever data system you like,
99 the merb core team does maintain plugins for the following Object Relational Mappers (ORM's):
100
101 ActiveRecord:: The same ORM that Rails uses. (<tt>sudo gem install merb_activerecord</tt>)
102 DataMapper:: Fairly new ORM. (<tt>sudo gem install merb_datamapper</tt>)
103 Sequel:: Fairly new ORM. (<tt>sudo gem install merb_sequel</tt>)
104
105 To use your choice ORM, install the appropriate gem and uncomment the appropriate +use_orm+ line in
106 Merb.root/config/dependencies.rb
107
108 === Controllers
109
110 (Merb.root/app/controllers/*)
111
112 Merb controllers inherit from Merb::Controller and contain built in view/template rendering.
113 Incoming requests are usually mapped to a specific method in a controller.  For example, using
114 the default routes, a request to http://www.yourapp.com/posts/show/1 would call the the #show
115 method of your PostsController (and params[:id] would = 1.)
116
117 The return value of your action function gets sent back to the client as the view.  In most cases,
118 you are going to want to end your functions with a call to +render+.  By default, +render+ will
119 render the view template associated with your action (in default merb that would be
120 Merb.root/app/views/<controller>/<action>.html.erb - see the View section for more info.)
121
122 Controllers can be generated by calling Merb.root/script/generate controller ControllerName.
123 By default, generated controllers inherit from the Application class (Merb.root/app/controllers/application.rb)
124 which itself inherits from Merb:Controller.  Application is a good place to put code pertinent to all controllers.
125 An example would be setting a filter to check if a user is logged in or to preload user data for each controller.
126
127 ==== +before+ and +after+ filters
128
129 Use the +before+ method in your controllers. +before+ accepts either a symbol, string or a Proc/lambda object. If you give it a symbol it will call a method with the same name as the symbol. If you give it a proc that takes one argument it will call the proc with the current controller as that argument. You can use :only and :exclude as options to your filters to exclude or include actions from certain filters. :only and :exclude take :symbols or [:sym, :sam] array of symbols.
130
131         class Foo < Merb::Controller
132  
133           before :setup_user, :only => :foo
134           before lambda {|c| c.headers['X-Foo] = 'bar' }, :exclude => [:foo, :baz]
135
136           def setup_user
137             # blah blah
138           end
139
140           def foo
141             # blah
142           end
143
144           def regular_action
145             # blah
146           end
147
148         end
149
150 To stop the before filter chain you use throw :halt with a few options:
151
152         # halts the filter chain and calls filters_halted which you can override
153         # in your controller to specialize it.
154
155         throw :halt
156
157         # halts the filters and calls the method named after the symbol:
158
159         throw :halt, :other_action
160
161         # halts the filter chain and returns the result of the Proc being called
162
163         throw :halt, Proc.new{ |c| c.redirect "/foo" }
164
165         # halts the chain and returns whatever is in the string
166
167         throw :halt, "<h1>You don't have permissions dude!</h1>"
168
169         or even render templates:
170
171         throw :halt, render 'foo'
172         throw :halt, partial 'foo'
173
174 After filters accept a symbol, string or Proc and call that proc with the controller:
175
176         after Proc.new {|c| Tidy.new(c.body) }, :only => :index
177
178
179 === Views
180
181 (Merb.root/app/views/*)
182
183 A view can be loosely defined as any data sent back to the client (a "view" of your data.)
184 By default, Merb controllers send the return value of your controller action as the view.
185 The Controller#render method simply renders the specified view and returns it as a string.
186 By default, a call to +render+ without any options renders the view template associated
187 with that controller action.  Using the default ERB templating system, this means that a
188 call to +render+ in Posts#index would render the file Merb.root/app/views/posts/index.html.erb
189
190 ==== Layouts
191
192 (Merb.root/app/views/layout/*)
193
194 Layouts are generic templates in which your specific view templates are rendered.  A sample
195 layout could look like:
196
197   <html>
198     <head><title>My Application Layout</title></head>
199     <body>
200       <img src="header" />
201       <div id="content">
202         <%= catch_content :layout %>
203       </div>
204     </body>
205   </html>
206
207 By default, +render+ will look for a corresponding layout for your controller in the form
208 of Merb.root/app/views/layout/<controller>.html.erb .  If no specific layout is present,
209 +render+ will attempt to use Merb.root/app/view/layout/application.html.erb
210
211 <i>See #render for more details/options, as well as how to use different templating systems
212 in your app.</i>
213
214 You can return several different types of values from your controller actions:
215
216 * String: Any string will get sent to the browser as standard text/html
217 * File/IO: Any file descriptor will get handed over to mongrel to be streamed to the client.
218 * Proc Object: The object will be called and the return value sent to the client.
219
220 That last point has some cool connotations if you think about it. Merb does
221 have a mutex lock around the call to your controller’s action anywhere that
222 you can call AR objects. Merb’s lock is way smaller then rails giant lock
223 though and allows for many more concurrent requests to be handled by one
224 process. By returning a Proc object from your action, you allow merb to
225 release the lock and the proc is called in multi threaded way. This allows
226 for all kinds of cool streaming and ‘futures’ where you return the proc and
227 release the mutex. It’s basically like handing over the proc to mongrel and
228 mongrel handles calling it in a thread safe manner.
229
230 === Helpers
231
232 (Merb.root/app/helpers/*)
233
234 app/helpers/global_helper.rb will be available to all of your views.
235  Helpers named after your controller plus _helper.rb will be included in the views
236  for that controller only.
237
238 === File uploads
239 When a file is uploaded with Merb, it gets put in a Tempfile. So
240 you just want to copy it to the right place on the filesystem.
241
242         def upload
243           puts params[:file].inspect
244           FileUtils.mv params[:file][:tempfile].path, Merb.root+"/uploads/#{params[:file][:filename]}"
245           render
246         end
247
248 A file upload will have a hash of params like this:
249
250         {
251         :filename => File.basename(filename), 
252         :content_type => content_type, 
253         :tempfile => <Tempfile>,
254         :size => File.size(body)
255         }
256
257 == Merb app layout
258
259         merb_app:
260
261           app
262             controllers
263             helpers
264             mailers         
265             models
266             parts
267             views
268           config
269           gems
270           lib
271           log
272           public
273           Rakefile
274           script
275           spec
276           test
277             unit
Note: See TracBrowser for help on using the browser.