| 1 |
= Merb |
|---|
| 2 |
|
|---|
| 3 |
==== FrameWork Development Dependencies |
|---|
| 4 |
|
|---|
| 5 |
Install these gems first: |
|---|
| 6 |
|
|---|
| 7 |
* mongrel |
|---|
| 8 |
* json |
|---|
| 9 |
* json_pure |
|---|
| 10 |
* erubis |
|---|
| 11 |
* mime-types |
|---|
| 12 |
* rspec |
|---|
| 13 |
* hpricot |
|---|
| 14 |
* mocha |
|---|
| 15 |
* rubigen |
|---|
| 16 |
* haml |
|---|
| 17 |
* markaby |
|---|
| 18 |
* mailfactory |
|---|
| 19 |
* Ruby2Ruby |
|---|
| 20 |
|
|---|
| 21 |
Then you can build the merb gem from svn trunk like so: |
|---|
| 22 |
|
|---|
| 23 |
$ sudo gem install mongrel json json_pure erubis mime-types rspec hpricot mocha rubigen haml markaby mailfactory Ruby2Ruby -y |
|---|
| 24 |
$ svn co http://svn.devjavu.com/merb/trunk merb |
|---|
| 25 |
$ cd merb |
|---|
| 26 |
$ rake install |
|---|
| 27 |
|
|---|
| 28 |
To generate a new merb app after the gem is installed: |
|---|
| 29 |
|
|---|
| 30 |
$ merb myapp |
|---|
| 31 |
|
|---|
| 32 |
=== The +merb+ server |
|---|
| 33 |
|
|---|
| 34 |
right now you add your routes in |
|---|
| 35 |
the appdir/config/router.rb file. So by default it runs on port 4000 |
|---|
| 36 |
|
|---|
| 37 |
$ cd /path/to/your/merb/app |
|---|
| 38 |
$ merb |
|---|
| 39 |
|
|---|
| 40 |
Or to start merb on a different port: |
|---|
| 41 |
|
|---|
| 42 |
$ merb -p 3500 |
|---|
| 43 |
|
|---|
| 44 |
To start a cluster of merb servers you specify the first port and then how many |
|---|
| 45 |
servers you want spawned. SO this command will start a merb instance on ports |
|---|
| 46 |
3000, 3001, 3002 |
|---|
| 47 |
|
|---|
| 48 |
$ merb -p 3000 -c 3 |
|---|
| 49 |
|
|---|
| 50 |
To start a Merb IRB console where all your models and other classes are pre loaded |
|---|
| 51 |
use the -i flag |
|---|
| 52 |
|
|---|
| 53 |
$ merb -i |
|---|
| 54 |
|
|---|
| 55 |
To see all the available command line flags use: |
|---|
| 56 |
|
|---|
| 57 |
$ merb -h |
|---|
| 58 |
|
|---|
| 59 |
=== Controllers |
|---|
| 60 |
|
|---|
| 61 |
Classes with built in render method and template handling |
|---|
| 62 |
with instance vars available in the views automatically. Merb also supports |
|---|
| 63 |
layouts. It will look for a layout named after your controller class first and |
|---|
| 64 |
then fall back to application.html.erb if no layout exists named after your |
|---|
| 65 |
controller. You can use render <tt>:layout => :none</tt>. |
|---|
| 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. |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
==== Before and after filters |
|---|
| 94 |
|
|---|
| 95 |
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 actionsfrom certain filters. :only and :exclude take :symbols or [:sym, :sam] array of symbols. |
|---|
| 96 |
|
|---|
| 97 |
class Foo < Merb::Controller |
|---|
| 98 |
|
|---|
| 99 |
before :setup_user, :only => :foo |
|---|
| 100 |
before lambda {|c| c.headers['X-Foo] = 'bar' }, :exclude => [:foo, :baz] |
|---|
| 101 |
|
|---|
| 102 |
def setup_user |
|---|
| 103 |
# blah blah |
|---|
| 104 |
end |
|---|
| 105 |
|
|---|
| 106 |
def foo |
|---|
| 107 |
# blah |
|---|
| 108 |
end |
|---|
| 109 |
|
|---|
| 110 |
def regular_action |
|---|
| 111 |
# blah |
|---|
| 112 |
end |
|---|
| 113 |
|
|---|
| 114 |
end |
|---|
| 115 |
|
|---|
| 116 |
To stop the before filter chain you use throw :halt with a few options: |
|---|
| 117 |
|
|---|
| 118 |
# halts the filter chain and calls filters_halted which you can override |
|---|
| 119 |
# in your controller to specialize it. |
|---|
| 120 |
|
|---|
| 121 |
throw :halt |
|---|
| 122 |
|
|---|
| 123 |
# halts the filters and calls the method named after the symbol: |
|---|
| 124 |
|
|---|
| 125 |
throw :halt, :other_action |
|---|
| 126 |
|
|---|
| 127 |
# halts the filter chain and returns the result of the Proc being called |
|---|
| 128 |
|
|---|
| 129 |
throw :halt, Proc.new{ |c| c.redirect "/foo" } |
|---|
| 130 |
|
|---|
| 131 |
# halts the chain and returns whatever is in the string |
|---|
| 132 |
|
|---|
| 133 |
throw :halt, "<h1>You don't have permissions dude!</h1>" |
|---|
| 134 |
|
|---|
| 135 |
or even render templates: |
|---|
| 136 |
|
|---|
| 137 |
throw :halt, render 'foo' |
|---|
| 138 |
throw :halt, partial 'foo' |
|---|
| 139 |
|
|---|
| 140 |
After filters accept a symbol, string or Proc and call that proc with the controller: |
|---|
| 141 |
|
|---|
| 142 |
after Proc.new {|c| Tidy.new(c.body) }, :only => :index |
|---|
| 143 |
|
|---|
| 144 |
=== Helpers |
|---|
| 145 |
|
|---|
| 146 |
app/helpers/global_helper.rb will be available to all of your views. |
|---|
| 147 |
Helpers named after your controller plus _helper.rb will be included in the views |
|---|
| 148 |
for that controller only. |
|---|
| 149 |
|
|---|
| 150 |
=== File uploads |
|---|
| 151 |
When a file is uploaded with Merb, it gets put in a Tempfile. So |
|---|
| 152 |
you just want to copy it to the right place on the filesystem. |
|---|
| 153 |
|
|---|
| 154 |
def upload |
|---|
| 155 |
puts params[:file].inspect |
|---|
| 156 |
FileUtils.mv params[:file][:tempfile].path, MERB_ROOT+"/uploads/#{params[:file][:filename]}" |
|---|
| 157 |
render |
|---|
| 158 |
end |
|---|
| 159 |
|
|---|
| 160 |
A file upload will have a hash of params like this: |
|---|
| 161 |
|
|---|
| 162 |
{ |
|---|
| 163 |
:filename => File.basename(filename), |
|---|
| 164 |
:content_type => content_type, |
|---|
| 165 |
:tempfile => <Tempfile>, |
|---|
| 166 |
:size => File.size(body) |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
== Merb app layout |
|---|
| 170 |
|
|---|
| 171 |
merb_app: |
|---|
| 172 |
|
|---|
| 173 |
app |
|---|
| 174 |
controllers |
|---|
| 175 |
models |
|---|
| 176 |
views |
|---|
| 177 |
parts |
|---|
| 178 |
mailers |
|---|
| 179 |
config |
|---|
| 180 |
lib |
|---|
| 181 |
public |
|---|
| 182 |
schema |
|---|
| 183 |
Rakefile |
|---|
| 184 |
scripts |
|---|
| 185 |
test |
|---|
| 186 |
spec |
|---|
| 187 |
unit |
|---|
| 188 |
plugins |
|---|