Month: April 2008

monkeypatching merb

Posted by on April 7, 2008

If you never heard of merb, stop reading this and go check it out!!!

One of things you will encounter when using the coolest and latest technologies is that they might contain bugs ;). It is what we encountered in using datamapper 0.2.5 and do_postgres 0.2.3 in our merb 0.9.2 development stack for ShellShadow.

Here’s a quick look at how we add our patches into our merb app.

One of nasty bugs existing in datamapper 0.2.5 or 0.3.0 is that it will interpret NULL integer fields from a table as 0. It was a huge issue for us and had to be patched.

Turns out the culprit is not datamapper but in do_postgres. Many thanks to Adam French for pointing this out on #datamapper irc channel.

The buggy code was in [GEM_PATH]/gems/do_postgres-0.2.3/lib/do_postgres.rb in class Reader < DataObject::Reader method typecast(val, field_type).

The code in questions was:

when "INT2", "INT4", "OID", "TID", "XID", "CID", "INT8"
val.to_i

This should read:

when "INT2", "INT4", "OID", "TID", "XID", "CID", "INT8"
val == "" ? nil : val.to_i

The fix was a one liner but we needed to replace the entire method it was in.

This is how you apply your monkeypatch:

  1. Add your patch file under <your_merb_project>/lib. Lets call it do_postgres_patch.rb. We put our modified copy of typecast in this file.
  2. In <your_merb_project>/config/init.rb, you’ll see at the very bottom

  3. Merb::BootLoader.after_app_loads do
    ### Add dependencies here that must load after the application loads:
    # dependency "magic_admin" # this gem uses the app's model classes
    end

    In this block, add:
    require 'core_extensions'

That’s it. Easy and all patched up!!!

ruby debugging with Aptana

Posted by on April 7, 2008

I’m pleased to report that Aptana Studio v1.1 ruby debugging works very well with merb and datamapper.

Last October, I tried every ruby debugger I could find (except the windows-only Sapphire Steele). At that time, only NetBeans 6 beta had any form of working debugger and for the most part, the command line ruby-debug was a better choice.

We were re-writing ShellShadow in merb (the old site was Rails).
Now that I’m exploring merb and datamapper and sequel, I find I need a GUI debugger. Unless you have an internalized mental model of the framework (e.g. you’re the creator), you won’t get far fast enough with command line debugging.
Aptana’s latest release on what was RadRails does the job.

Here are a few things to get you going:

  1. Download and install Aptana. This is a multi-step process where you need to install Aptana and then from the intro page, install the RadRails plugin. There may be updates to Aptana. Just let eclipse work its magic and update to the latest patches.
  2. install ruby-debug-ide.
    gem install ruby-debug-ide. You will want to run this as “sudo gem install ruby-debug-ide” if you are installing to your global gems setup.
  3. On OS X 10.5, I needed to create a link (the following is a one line command)
    sudo ln -s /usr/bin/rdebug-ide /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/rdebug-ide
  4. Config the ruby debugger in Aptana. Open the eclipse/aptana preferences dialog from “Windows->Preferences…”. In the section “Ruby>Debugger”, turn on the checkbox “Use ruby-debug library.
  5. Setup your app. I didn’t create a Rails project. I create a ruby project (its for merb). I have my aptana/eclipse workspace outside of my ruby project and simply point the working directory of my project to the right place. You’ll want to set your debug setting for this project to run /usr/bin/merb or whatever your app is. This approach litters my merb project directory with two files: .loadpath and .project. You’ll probably want to tell your version control system of choice to ignore these files.

Its not perfect, but this seems to be as good as it gets at the moment for a GUI ruby debugger. Here are a few things of interest:

  1. Breakpoint removal is flaky. We found that sometimes when you turn off a breakpoint, it doesn’t go away. We had to edit the code to force aptana/eclipse to refresh itself.
  2. Interactive console isn’t great. IRB has a cleaner expereince but the apatana console does work.
  3. Missing ability to eval expressions in the debugger. This would be a great feature and hopefully it gets added someday.

Many thanks to the Aptana RadRails team!!