First, I installed Barista, which is a nifty Ruby gem that adds transparent CoffeeScript support to any Rack app. There were a few other gems available, however, I decided that Barista had the most mature documentation and the project still showed signs of life.
Once I added the Barista gem to my Gemfile and ran Bundler, I next had to require Barista in my Sinatra app; moreover, I had to register the Barista extension via
Sinatra.register Barista::Integration::Sinatra. Note, that these steps need to be done after Haml is loaded.
Accordingly, since this application runs on Ubuntu, I got to use a nifty script that I happened to have written about a year ago: Ubuntu Equip. In less than a minute, I had the latest and greatest version of Node running by running the command:
Take note: this script sets a custom apt-get repository; otherwise, if you don’t add this repository, you’ll end up with an ancient version of Node.
Finally, to actually start coding in CoffeeScript, all you have to do is use Haml’s handy inline
That’s it! The steps required to get Sinatra, CoffeeScript, and Haml playing together are:
- Install Barista
- Configure Sinatra
- Write some CoffeeScript
In short, Barista and Node make Sinatra swing with CoffeeScript.