Here we are going cover the install instructions for Merb, RSpec, and DataMapper (0.9) and how to create a bare application. The next post will cover aspects of the framework and introduce the example application we will be building.
Before we get started I'm going to assume you have the following installed:
If you have an older version of Merb (<0.9.2) you should remove the all the gems before continuing. Use
gem list to see your installed gems.
merb gems should be as simple as:
1 sudo gem install merb --source http://merbivore.org
Unfortunately we are living right on the edge of development so we'll need to get down and dirty with building our own gems from source. Luckiliy this is much easier than it sounds...
Start by installing the
1 sudo gem install rack mongrel json_pure erubis mime-types rspec hpricot \ 2 mocha rubigen haml markaby mailfactory ruby2ruby
Then download the
1 git clone git://github.com/wycats/merb-core.git 2 git clone git://github.com/wycats/merb-plugins.git 3 git clone git://github.com/wycats/merb-more.git
Then install the gems via rake:
1 cd merb-core ; rake install ; cd .. 2 cd merb-more ; rake install ; cd .. 3 cd merb-plugins; rake install ; cd ..
json_pure gem is needed for merb to install on JRuby (Java implementation of a Ruby Interpreter), otherwise use the
json gem as it's faster.
Merb is ORM agnostic, but as the title of this book suggests we'll be using DataMapper.
Should you want to stick with ActiveRecord or play with Sequel, check the merb documentation for install instructions.
DataMapper is splitting into
datamapper 0.3 will be outdated soon.
If you have an older version of
merb_datamapper (< 0.9) you should remove them first. Remove the
merb_datamapper gem before installing
We will use MySQL in the following example, but you can use either sqlite3 or PostgreSQL, just install the appropriate gem. You will also need to ensure that MySQL is on your system path for the gem to install correctly.
(TODO) - gem instructions for DM 0.9 once they are released
To get the gems from source:
1 git clone git://github.com/sam/do.git 2 3 cd do 4 cd data_objects 5 rake install ; cd .. 6 cd do_mysql # || do_postgres || do_sqlite3 7 rake install 8 9 git clone git://github.com/sam/dm-core.git 10 git clone git://github.com/sam/dm-more.git 11 12 cd dm-core ; rake install ; cd .. 13 cd dm-more 14 cd dm-merb ; rake install ; cd .. 15 cd dm-validations ; rake install ; cd ..
To update a gem from source, run
git pull and
rake install again.
rspec gem was installed in the Merb section above. However, if for some reason you didn't install it there, or want to grab the it from source, run the following commands:
1 gem install rspec 2 svn checkout http://rspec.rubyforge.org/svn/trunk rspec_trunk
Creating an App
Now that we've got all of that installed, time to create a test Merb application. Merb follows the same naming convention for projects as rails, so 'my_test_app' and 'Test2' are valid names but 'T 3' is not (they need to be valid SQL table names).
1 merb-gen app test
This will generate an empty Merb app, so lets go in and take a look. You'll notice that the directory structure is similar to Rails, with a few differences.
1 # expected output 2 RubiGen::Scripts::Generate 3 create app 4 create app/controllers 5 create app/helpers 6 create app/views 7 create app/views/exceptions 8 create app/views/layout 9 create autotest 10 create config 11 create config/environments 12 create public 13 create public/images 14 create public/stylesheets 15 create spec 16 create app/controllers/application.rb 17 create app/controllers/exceptions.rb 18 create app/helpers/global_helpers.rb 19 create app/views/exceptions/internal_server_error.html.erb 20 create app/views/exceptions/not_acceptable.html.erb 21 create app/views/exceptions/not_found.html.erb 22 create app/views/layout/application.html.erb 23 create autotest/discover.rb 24 create autotest/merb.rb 25 create autotest/merb_rspec.rb 26 create config/rack.rb 27 create config/router.rb 28 create config/init.rb 29 create config/environments/development.rb 30 create config/environments/production.rb 31 create config/environments/rake.rb 32 create config/environments/test.rb 33 create public/merb.fcgi 34 create public/images/merb.jpg 35 create public/stylesheets/master.css 36 create spec/spec.opts 37 create spec/spec_helper.rb 38 create /Rakefile
Before we get the server running, you'll need to edit the init.rb file and un-comment the following lines (this is only necessary if you need to connect to a database, which we do in our case):
1 use_orm :dm_core 2 3 use_test :rspec
merb now in your command line will try and start the server.
1 Started merb_init.rb ... 2 No database.yml file found in /Users/work/merb/example_one/config. 3 A sample file was created called database.sample.yml for you to copy and edit.
As you can see, we forgot to set up the database. A sample file has helpfully been generated for us. Edit this and rename it to database.yml:
1 # This is a sample database file for the DataMapper ORM 2 development: 3 adapter: mysql 4 database: test 5 username: root 6 password: 7 host: localhost 8 socket: /tmp/mysql.sock
Don't forget to specify your socket, if you do not know it's location, you can find it by typing:
1 mysql_config --socket
Starting Merb again shows that everything is running okay.
The following command will give you access to the Merb console:
1 merb -i
You'll notice Merb runs on port 4000, but this can be changed with flag
-p [port number]. More options can be found by typing:
1 merb --help
You can even run Merb with any application server that supports rack (thin, evented_mongrel, fcgi, mongrel, and webrick):
1 merb -a thin