Sat, 05 Jul 2008

Getting started with Merb and Datamapper

Posted by Ben Sat, 05 Jul 2008 22:46:00 GMT

Merb is the up-and-coming Ruby web framework, designed to improve upon the ideas in Rails whilst being faster, lighter and less opinionated (or more agnostic). I wanted to discover whether it provides any benefits over Rails and what it does differently. The best way to find out; get it installed it and create a new app.

Update: There’s now a much easier way of getting Merb & DataMapper installed.

The official Merb website gives the following simple installation instructions.

sudo gem install merb

However, since I wanted to also try DataMapper as the ORM those instructions failed to install a working setup due to a conflict between Merb and DataMapper dependencies. Unperturbed, I finally got them both installed and working happily together by getting the bleeding edge code from each project’s github repository and following the instructions provided by Mathew Ford’s Living life on the edge. The book is also available on github, you can contribute patches there. Hopefully this issue will be resolved soon enough so that you’ll be able to install the official release gems for both Merb and DataMapper from Rubyforge (instead of running on edge).

Prerequisites

  • You must have git installed (sudo port install git-core using MacPorts on OS X).
  • Remove any older versions of merb (and datamapper) that you might have installed. Use gem list to view local gems and sudo gem uninstall <name> to remove each individual gem.

The Easy Way

Matt’s book outlines an easy way of installing the latest merb & datamapper source, however it failed to properly install the DataMapper gem for me so I had to resort to the hardcore approach.

sudo gem install sake
sake -i 'http://edgy.4ninjas.org/edgy.sake'
sake edgy:install packages="merb-stack"

After installing, to keep up-to-date you just need to execute:

sake edgy:update

Now, if you’re hardcore…

Installing the required dependencies

Start by installing the gem dependancies.

sudo gem install rack mongrel json erubis mime-types rspec hpricot mocha rubigen haml markaby mailfactory ruby2ruby

Installing Merb

Download the merb source:

git clone git://github.com/wycats/merb-core.git
git clone git://github.com/wycats/merb-plugins.git
git clone git://github.com/wycats/merb-more.git

Then install the gems via rake:

cd merb-core ; rake install ; cd ..
cd merb-more ; rake install ; cd ..
cd merb-plugins; rake install ; cd ..

Installing DataMapper

git clone git://github.com/sam/extlib.git  
git clone git://github.com/sam/do.git

cd extlib ; rake install ; cd ..
cd do
cd data_objects ; rake install ; cd ..
cd do_mysql ; rake install ; cd ..
cd ..

git clone git://github.com/sam/dm-core.git
git clone git://github.com/sam/dm-more.git

cd dm-core ; rake install ; cd ..
cd dm-more ; rake install ; cd ..

Keeping updated

To update your local gems with the latest code changes you’ll need to do a git pull and then rake install for each of the git repositories previously downloaded.

Creating your first Merb app

You create a new empty Merb app via the merb-gen utility.

merb-gen app demo
(You can also create flattened or single file applications if you wish).
merb-gen app app_name --flat      (for a flattened application)
merb-gen app app_name --very-flat (for a single file application)

As we’re using DataMapper we need to edit config/init.rb and uncomment the use_orm :datamapper line. The next step is to create the database settings file config/database.yml:

---
development: &defaults
  adapter:    mysql
  database:   demo_development
  username:   demo
  password:        p@ssword
  host:       localhost

test:
  <<:         *defaults
  database:   demo_test

production:
  <<:         *defaults
  database:   demo_production

You can now fire up the Merb server to make sure it runs.

$ merb
 ~ Loaded DEVELOPMENT Environment...
 ~ Connecting to database...
 ~ loading gem 'merb_datamapper' ...
 ~ Compiling routes...
 ~ Using 'share-nothing' cookie sessions (4kb limit per client)
 ~ Using Mongrel adapter

Some other useful commands to know:

merb -i                                                        # Interactive merb console

merb-gen model <name>                            # Creates a Datamapper Model stub
merb-gen resource <name>                    # Creates a full resource (incuding model, controller, views & test spec)

rake dm:db:automigrate            # Perform automigration
rake dm:db:autoupgrade            # Perform non destructive automigration

Let’s see how it goes from here with Merb!

Update: There’s now a much easier way of getting Merb & DataMapper installed.

Comments

Leave a response

  1. Jason Green 21 days later:
  2. Jason Green 21 days later:

    Sorry somehow the text didn’t get added there, I wrote a quick article on gems and sake tasks Have a look here: http://www.nogeek.org/2008/07/25/using-sake-to-keep-merb-and-datamapper-on-edge/

Comments