Articles tagged 'merb'

Light as a Feather Apr 7 2008

My boy Mike is back blogging his ass off again, and this time, he’s doing it on some decent software! Last few weeks me and Mike have been working on an ultra-cool blogging platform written using the Merb framework, and it’s now in production use for Mike’s blog. This very blog will follow suit shortly as I migrate over, and within a few weeks we are hoping to have the codebase in a good enough state to be able to open up the source, and start to invite contributions from anyone that feels the need to help out. The idea behind Feather is the same as Merb - lightweight core framework, extended heavily by plugins, allowing you to choose the pieces of functionality you need, and leave out the guff that you don’t. Keep it dialed in here for further updates on Feather over the next few weeks, and keep a keen eye on Mike’s blog too for more great posts. In fact, he’s already in the thick of it with this great post on why Ruby does it for him (and why .Net doesn’t).

Comments

Adventures in Merb Mar 17 2008

I decided to give Merb a go, and after doing a fair bit of reading up on it, I took the plunge. It’s a little rough around the edges, but that’s probably to be expected given it’s youth - however there are a number of cool features and benefits to using it, and it’s getting better and better as it’s being refactored. I did however come across a few interesting issues getting up and running, so I thought I’d share these to try and make the process easier for others.



I installed the gems from http://merbivore.com, by doing the following:



sudo gem install merb --source=http://merbivore.com --include-dependencies

This gave me the merb-core gem, along with merb-more. However, to get up and running with a database, we really need a database plugin - let’s try DataMapper.



sudo gem install datamapper --source=http://merbivore.com --include-dependencies
sudo gem install merb_datamapper --source=http://merbivore.com --include-dependencies

Now you’d think that a dependency for merb_datamapper would be datamapper itself, however it doesn’t get included, and so you need to download that as a gem, and then download merb_datamapper. We then need a few other gems, including one specific to the database you’ll be connecting to:



sudo gem install data_objects --source=http://merbivore.com
sudo gem install do_sqlite3 --source=http://merbivore.com

As you can see, I chose to install do_sqlite3 for accessing a sqlite3 database. However you could install “do_mysql”, or “do_postgres” to access MySql or PostgreSql databases respectively, and I’m sure there are others. Sqlite users using Mac OS X 10.4 (Tiger) should bear in mind an issue I came across, whereby a strange error is received when attempting to do any database related activity with the do_sqlite3 gem and the default native sqlite3 version that comes with the operating system. This manifested itself as a couple of different strange errors (including a “DataMapper constant not found” error when starting Merb), however the solution was simply to grab the latest sqlite source from www.sqlite.org, build it, and install it. Then try re-installing the do_sqlite3 gem as above, and you should find that they play together much better!



We can now proceed with creating a Merb app to play with:



merb-gen testapp

Looking in the “testapp” folder now shows us the initial structure Merb uses, which is fairly similar to how Rails applications are structured. The main differences that you’ll need to know straight away are that routes are defined in the “router.rb” file within the “config” directory, and that the main settings and application initialization stuff is within “init.rb” in the “config” folder. Let’s setup our app to use DataMapper by changing the “init.rb” file. Find the following lines:



### Uncomment for DataMapper ORM
# use_orm :datamapper

### Uncomment for ActiveRecord ORM
# use_orm :activerecord

### Uncomment for Sequel ORM
# use_orm :sequel

and uncomment the datamapper line, like this



### Uncomment for DataMapper ORM
use_orm :datamapper

### Uncomment for ActiveRecord ORM
# use_orm :activerecord

### Uncomment for Sequel ORM
# use_orm :sequel

This then tells our application to use and load the DataMapper database plugin. Further down in that file you’ll see something similar for testing, and you can choose whether to use rspec, or test_unit. Using rspec is the default. After we change the database settings, we run:



merb-gen

to get it to generate database config. This will create the file “database.sample.yml” in the “config” folder, which we can rename to “database.yml”, and configure correctly. Here is a sample development configuration set to use sqlite3:



:development: &defaults
:adapter: sqlite3
:database: db/testapp_development.sqlite3

Remember to create the “db” directory. Now to finish off our test application, we can create a dummy controller and views, to see our application in action.



merb-gen resource person first_name:string last_name:string date_of_birth:date

Now we need to migrate the data to take into account the new model. Note there are no migration files like in Rails, however there is a Rake task to migrate the data based on the DataMapper definitions with the model classes:

rake dm:db:automigrate

That should now create the database file (“db/testapp_development.sqlite3”), and we should now be able to play around with the model within our application. First, let’s run the application via the console, and inspect the new model:



merb -i
>> Person.count

This will display a count of 0, as there are no people in the database yet. Let’s create one:

>> Person.create({:first_name => "test", :last_name => "dude", :date_of_birth => Time.now})

Only this returns an error!



DataObject::QueryError: Your query failed.
table people has no column named date_of_birth

For some reason, the migrations within DataMapper tend to be a bit erratic - there is a console command you can run however that will often fix issues and sync the model definitions to the database more accurately:



>> DataMapper::Persistence::auto_migrate!

Running our create command from before should now work as expected, with a Person object returned and saved with an ID of 1. You can check to ensure that there is a Person object:



>> Person.count
=> 1
>> Person.all
=> [#Person]
>> Person.first
=> #Person
>> Person[1]
=> #Person

The above queries show how to retrieve all Person instances within the database, just the first, or a specific Person by ID. In this case, the same (and only) Person instance is returned (it’s returned as an array with a single element in the “Person.all” query). But now let’s check out the real deal, and see the web interface - fire it up by just running:



merb

This will start the server, and you can now browse to http://localhost:4000 to see the application. This will show a Merb error page, saying that no route matches the request “/”. This is because while we have a single resource within the application, it’s setup to be exposed by default at http://localhost:4000/people. Browse to that url, and you’ll see the default Merb generated view. Let’s hook this up to the root of the application so it’s accessible at the easier to remember url http://localhost:4000. Within the “router.rb” file in the “config” folder, look for the following lines:



# Change this for your home page to be available at /
# r.match('/').to(:controller => 'whatever', :action =>'index')

and change the second line to read the following (uncommenting it, and changing the controller to our “people” controller):



r.match('/').to(:controller => 'people', :action =>'index')

Now browsing to http://localhost:4000 will show you the same default view for the “people” index as it would if you were browsing http://localhost:4000/people. You can now customize these views to display and interact the models, in a similar fashion as you would do in Rails!



One other thing to bear in mind is that Merb apps default to port 4000 - however running different apps on that default port at differing times will cause issues because of the way the secret session key is managed. This will most likely be fixed in due course, but at the minute running a second app on the same port at a later date will often result in a secret key error - you can run each app on a different port, or simply find the cookie that represents the “lock” on that port by a specific application, and remove it; then a different application won’t have issues using that port. You may well find that re-using a Merb app port with a Rails app also causes an issue, again removing the cookie should fix this.



So that’s pretty much it - a very basic Merb app, some potential pitfalls found and sidestepped, and a powerful, lightweight web application framework at your fingertips. While it’s obvious Merb is a work in progress, it’s also obvious just how useful this framework might become, especially for those looking to create more streamlined, better performing applications.

Comments

Ruby link round-up Feb 24 2008

I’ve seen a number of great Ruby articles, tutorials and discussions recently (and not so recently), as well as some interesting projects, so I thought I’d write a quick round-up of links to some of the most interesting stuff. It’s split by category, with a miscellaneous list for stuff that didn’t seem to fit anywhere else.

  • Merb
    • Tutorial on getting up and running with Merb 0.9.0.
    • A list of great Merb articles.
    • An article from Ezra, the guy who created Merb, on using Merb’s integration with Rack to write handlers that run close to the http processing pipeline, while remaining webserver agnostic.
  • Amazon EC2 + S3
    • An article on using Amazon web services, Lucene and Ruby for web crawling.
    • This gem enables really easy setup of EC2 boxes for use with Rails. I’ve tried it. And it rocks.
  • iPhone
    • An article on hosting iPhone subdomains within your Rails app, to display iPhone specific versions of web applications.
    • This article goes into even greater detail on iPhone specific versions of Rails apps.
  • Books
    • Another post from Ezra, who happens to be one of the authors of ‘Deploying Rails Applications’. This post says that the book is now at the final beta stage, and includes some more advanced content such as Apache/Nginx scaling, and Xen setups. I’ve just ordered up the beta PDF, and will be receiving the paperback too when it ships in May. Hoping to do an initial review once I give the beta a read, looks very interesting though.
  • Tools and projects
    • Heroku, online Rails application creation and hosting. Very cool tool for creating or collaborating on a hosted Rails application, all through your browser.
    • Rush, a Ruby based shell, from one of the guys behind Heroku. Interesting concept, along the same sort of lines as Windows Powershell - but because it’s Ruby, it has a much better syntax :-)
    • An article about the usage of Beanstalk, a messaging queue system, to power the Nuby on Rails blog.
    • Hot off the presses, a new Capistrano preview, with some cool features such as better role definitions, and better git integration.
  • Miscellaneous
    • An article on generating feeds using microformats within html.
    • Good post from 37signals about building a web app, and making sure you get it right - even if it means starting over.
    • An article on using Ruby for scripting tasks on Mac OS X.
    • Interesting article on tackling master/slave database setups with Rails.
    • Great screencast on PeepCode about git, a source control tool that is all the rage at the minute. Really good introduction to the tool.
OK so that wasn’t such a quick round-up - but that was the best of the best from the articles and links I’ve come across over the last few months.

Comments

Page 2 of 2 | Previous page