title: Merb - All You Need, None You Don't gradient: top-bottom black grey
Ezra Zygmuntowicz, Engine Yard @ Mountain West RubyConf 2008
(Adapted S9 Version from Original PDF Slide Deck)
- Prefer simplicity over magic as much as possible
- This is framework code, no
&:fooor returning allowed! - When in doubt... benchmark and profile
- Know your runtime and how it acts
No code is faster than no code.
- Developer time is expensive...
- Servers are cheap...
Just throw more servers at the problem!
Throwing more servers at the problem only goes so far ...
Of course I'm happy to sell you more servers ;)
Premature Optimization is the root of blah blah blah...
Postmature Optimization is the root of all hosting bills ;)
- merb-core
- Rack Webserver Abstraction Layer
- Powerful Router
- Provides API
- merb-more
- merb-plugins
- Ebb
- Evented Mongrel
- FastCGI
- Mongrel
- Thin
- Webrick
config/rack.rb:
class ApiHandler
def initialize(app)
@app = app
end
def call(env)
request = Merb::Request.new(env)
if request.path =~ %r{/api/(.*)}
[200,
{"Content-Type" => "text/json"},
Api.get_json($1)]
else
@app.call(env)
end
end
end
use ApiHandler
run Merb::Rack::Application.new
Merb::Router.prepare do |r|
r.resources :posts do |post|
post.resources :comments
end
end
Named Routes:
| Name | Route |
|---|---|
| delete_post_comment | /posts/:post_id/comments/:id/delete |
| post_comments | /posts/:post_id/comments |
| new_post | /posts/new |
| posts | /posts |
| post_comments | /posts/:post_id/comments/:id |
| edit_post | /posts/:id/edit |
| post | /posts/:id |
| new_post_comment | /posts/:post_id/comments/new |
| delete_post | /posts/:id/delete |
| edit_post_comment | /posts/:post_id/comments/:id/edit |
Merb::Router.prepare do |r|
r.match(%r[^/foo/(.+)], :user_agent => /(MSIE|Gecko)/).
to(:controller => 'foo', :title => '[1]',
:action => 'show', :agent => ':user_agent[1]')
r.match('/bar/:baz').defer_to do |request, params|
if bar = Bar.find_by_baz params[:baz]
{:controller => bar.controller,
:action => bar.action,
:bar => bar.to_param}
end
end
end
Merb.add_mime_type(:yaml, :to_yaml, %w[application/x-yaml text/yaml])
Merb.add_mime_type(:html, :to_html, %w[text/html application/xhtml+xml application/html])
Merb.add_mime_type(:xml, :to_xml, %w[application/xml text/xml application/x-xml], :Encoding => 'UTF-8')
Merb.add_mime_type(:json, :to_json, %w[application/json text/x-json])
class Posts < Application
provides :json, :yaml, :xml
def show(id)
@post = Post.find id
display @post
end
end
- Code Generators
- Asset Bundling
- Mailers and Parts
- Haml and other templating support
- Action Args
- Caching
- Other essentials
- merb_datamapper
- merb_activerecord
- merb_sequel
- merb_helpers
- merb_param_protection
- many more to come
- github.com/wycats/merb-core/tree/master
- merb.lighthouseapp.com
- #merb irc.freenode.net
- groups.google.com/group/merb