Latest from the Bamboo Blog

Merb and DataMapper Book (Part 1) matt

April 9th, 2008

When I started learning Merb and DataMapper I kept a collection of notes to help me keep up with these projects. These grew to the point that they couldn't fit nicely into a single text file. With contributions from others I started to put together a small book on developing web apps with Merb and DataMapper. I have decided to release it as we go. There is also a project in GitHub if you want to check it out and contribute.

So here we go (corrections and comments welcome):

Life On The Edge With Merb, DataMapper & RSpec

What's Merb, DataMapper & RSpec?

If you're not living on the edge, you're taking up too much room. - Alice Bartlett

Merb, DataMapper and RSpec are all open source projects, which can be used for building kick-ass web applications. They are all in active development and it can be hard to keep up, but we'll try our best to keep up to date.

Merb

It's a relatively new framework (a bit like Ruby on Rails) and was created by Ezra Zygmuntowicz. Merb stands for Mongrel + ERB although now it supports the rack webserver interface so it can user any web server that has rack support (Mongrel, Thin, ebb, etc).

If you know Ruby and have used Rails you're likely to get the hang of Merb quite easily. Noticeable differences from Merb to Rails are, it's stance on being less opinionated and it's approach to modularisation.

Merb is actually made up of a number of gems: merb-core, merb-more and merb-plugins. This is so you can pick and choose the functionality you need. The merb gem installs both merb-core and merb-more so you can get started straight away. The benefit of modularity is it's kept simple with additional functionality provided by gems.

merb-core alone can be used as an upload server, to provide an API or to create a simple web app (a la camping) where the functionality of a fully fledged framework isn't necessary.

DataMapper

DataMapper is an Object-Relational Mapper (ORM) written in Ruby, by Sam Smoot. It's what we'll be using with Merb. It's possible to use the same ORM as Rails (ActiveRecord), but as there are plenty of examples of using ActiveRecord already I've chosen to use DataMapper.

It has some nice features which makes it faster than ActiveRecord in some cases. What really stands out for me is the way it handles database attributes. The schema, migrations and attributes are all defined in one place: your model. So you no longer have to look around in your database or other files to see what is defined.

DataMapper has some similarities with ActiveRecord. We will be highlighting the differences as we go along.

RSpec

RSpec is a Behaviour Driven Development framework for Ruby. Merb currently supports the Test::Unit and RSpec testing frameworks. As the specs for Merb and Datamapper are written in RSpec, we will be covering some aspects of RSpec but it will not be our main focus.

What About Ruby On Rails?

[Merb is] Harder, Better, Faster, Stronger, to quote Daft Punk - Max Williams

So what's the big deal? We have Ruby on Rails and that's enough, isn't it? There is little doubt that Ruby on Rails has rocked the web application development world. You have to give credit where credit's due, but it can be unforgiving if you don't want to do things 'the Rails way'.

Where Rails is opinionated, Merb is agnostic. You can easily use your favourite ORM (ActiveRecord, DataMapper, Sequel) or none at all, Javascript Library and template language.

Merb also has super-fast routing and is thread-safe (If performant were a word, Merb would be it). The core functionality is kept separate from the other plugins and it uses less Ruby 'magic', which makes it easy to understand and hack.

Rails (and consequently Ruby) has received a lot of criticism for not being suitable for large scale web applications, which isn't necessarily true and Merb has been built from the outset to prove that Ruby is a viable language for building fast and scaleable web applications.

At the end of the day it's about choice. There are many new Ruby frameworks springing up, undoubtedly helped by the success of Rails, but in my opinion Merb shows the most promise as a Rails competitor.

If you'd like to take a look at some other frameworks these links should get you started:


So that's it for now, stay tuned for how to install/set-up merb and datamapper next...

5 Responses to “Merb and DataMapper Book (Part 1)”

  • jesse

    Nice intro, and I’m psyched to read more. I’m getting ready to build an upload-intensive app and was going to use Rails, but decided I wanted it all in one framework. Merb + DataMapper seems like a really solid choice.

  • Steve Tooke

    Great work! Really looking forward to reading more as it develops.

  • jaigouk

    I really appreciate your work. I’m new to merb and couldn’t find any help videos.(I hope peepcode will release it soon). RoR was good to me at first, but deploying it was so painful and slow. Thank you for your work.

  • phantom

    i’ve been doing a fair bit of research and it seems that activerecord is the only orm that’s available on windows for sqlserver.

    i’d particularly like to use datamapper, but it appears to be some way off yet – does anybody know differently ?

  • Sam Smoot

    @phantom:

    SQL Server support really isn’t that far off, but you have to either be willing to accept the same drawbacks as AR+ADO (through WIN32OLE) (read: poor NVARCHAR support, please correct me if this has been resolved), or know a little C to write a DataObjects driver for ADODB’s VC6 interface.

    It’s really not as brutal as it sounds. Checkout the MySQL driver: http://github.com/sam/do/tree/master/do_mysql/ext/do_mysql_ext.c

    725 lines and a very large portion of those is declarations, Ruby bindings and type-casting code that’s mostly copy/paste/tweak-a-few-lines between the different drivers. You could start with that code as a basis and the final SQL Server driver probably wouldn’t be more than 200 lines difference.

    So a day’s worth of work perhaps. If no one in the Microsoft community steps forward to make a DataObjects::SqlServer driver then the DataMapper team will in the not too distant future.

    It’s my goal to add native DB2, Oracle and MS SQL Server support to DataObjects as soon as I can steal enough time away from other efforts.

Sorry, comments are closed for this article.