SuperModel – to sexy up your Ruby models

11 Jun 2008 – Singapore

A few months ago, my friend Chu Yeow and I released a Ruby gem called ActiveCouch which was designed to be a very elegant Ruby wrapper for the exciting new phenomenon – CouchDB. ActiveCouch was my first shot at a Ruby library and I had a lot of fun creating elegant DSL’s (a lot of which were inspired from ActiveRecord and ActiveResource). In essence, ActiveCouch lets you define models like so:

While this is great, I found myself using these semantics more often in other Ruby libraries that I started to work on. Thus was born – SuperModel.

SuperModel’s aim is to wrap semantics such as has, has_one, has_many (which are familiar to many Ruby/Rails developers) in a re-usable library which can form the basis for other such libraries. For instance, I am currently in the process of re-writing ActiveCouch so that ActiveCouch::Base inherits from SuperModel::Base. By doing so, all the ‘modeling’ semantics will be taken care of by SuperModel and ActiveCouch can deal with interfacing with CouchDB.

SuperModel also gives you serialization for free (currently only JSON, but future releases will also include XML, YAML, etc.) So, methods such as to_json, to_xml and to_yaml will let you serialize/de-serialize SuperModel objects in/from any format that you want.

SuperModel is hosted at http://www.github.com/arunthampi/supermodel and the gem is available from GitHub as well. [Note: There are some problems currently with installing gems from GitHub, please refer to the GitHub RubyGems page for pointers]

About

The author of this blog is a Ruby Developer based in Singapore. Co-Founder of Gameplan, dabbles in Ruby, CouchDB and anything new and shiny (shininess is mandatory).

Loves Mac OS X and the iPhone.

Supports Manchester United and is a fan of Mohanlal, Quentin Tarantino, The Coen Brothers and Seth Rogen.

Mostly listens to Indian music, but loves The Beatles, The Doors, Guns n' Roses and Dire Straits.

The author's pseudonym 'McLovin' is inspired from the 2007 classic Superbad.

I also post occasionally on my Posterous.

Projects

Subscribe to mclov.in