Skip to content

Latest commit

 

History

History
180 lines (128 loc) · 3.79 KB

File metadata and controls

180 lines (128 loc) · 3.79 KB

title: Merb - All You Need, None You Don't gradient: top-bottom black grey

Merb - All You Need, None You Don't

Ezra Zygmuntowicz, Engine Yard @ Mountain West RubyConf 2008

(Adapted S9 Version from Original PDF Slide Deck)

Guiding Principles of Merb Development

  • Prefer simplicity over magic as much as possible
  • This is framework code, no &:foo or returning allowed!
  • When in doubt... benchmark and profile
  • Know your runtime and how it acts

Merb's Motto

No code is faster than no code.

Why? Why Not?

  • Developer time is expensive...
  • Servers are cheap...

Who Cares About Efficiency

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 ;)

Who Cares About Efficiency Continued

Premature Optimization is the root of blah blah blah...

Postmature Optimization is the root of all hosting bills ;)

What's New?

  • merb-core
    • Rack Webserver Abstraction Layer
    • Powerful Router
    • Provides API
  • merb-more
  • merb-plugins

merb-core - Rack Webserver Abstraction Layer

  • 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-core - Powerful Router

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-core - Powerful Router Continued

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-core - Provides API

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

merb-more - What's New?

  • Code Generators
  • Asset Bundling
  • Mailers and Parts
  • Haml and other templating support
  • Action Args
  • Caching
  • Other essentials

merb-plugins - What's New?

  • merb_datamapper
  • merb_activerecord
  • merb_sequel
  • merb_helpers
  • merb_param_protection
  • many more to come

Contribute!