Monday, July 14, 2008

leopard, rubygems, and bus errors

Every now and then I catch one of these errors:
1
2
3
4
5
6
~/git/project_foo $ rake -T
(in /Users/jackdempsey/git/project_foo)
/usr/local/lib/ruby/gems/1.8/gems/libxml-ruby-0.3.8.4/lib/xml/libxml_so.bundle: [BUG] Bus Error
ruby 1.8.6 (2007-09-24) [i686-darwin9.0.0]

Abort trap



In my experience this just means that you've done some custom stuff to ruby on your Leopard install, and the bundled gems that come with Leopard these days aren't happy. Simple fix, just uninstall the gem and reinstall. This gives things a chance to build the extensions correctly, and be happy from then on:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
~/git/project_foo $ gem list lib

*** LOCAL GEMS ***

libxml-ruby (0.3.8.4)
~/git/project_foo $ sudo gem uninstall libxml-ruby
Successfully uninstalled libxml-ruby-0.3.8.4
~/git/project_foo $ sudo gem install libxml-ruby
Building native extensions. This could take a while...
Successfully installed libxml-ruby-0.7.0
1 gem installed
Installing ri documentation for libxml-ruby-0.7.0...
Installing RDoc documentation for libxml-ruby-0.7.0...
~/git/project_foo $ rake -T


(things show nicely now)

Thursday, July 10, 2008

logging to a file in production merb

This comes up often in #merb. You can use several flags to customize things nicely:

merb -e production -l debug -d

This sets merb to run in the production environment, to use a log level of debug (use merb --help to see the others), and -d to daemonize and not log things to the console window.

introducing merb-thor

About a year ago I was working with an ActiveRecord model, and for some reason things would blow up when I tried to do anything to this model inside rake. Turns out having a field called 'link' was a very bad idea, given the rake code bel
1
2
3
4
5
6
7
8
9
10
# ###########################################################################
# Include the FileUtils file manipulation functions in the top level module,
# but mark them private so that they don't unintentionally define methods on
# other objects.

include RakeFileUtils
private(*FileUtils.instance_methods(false))
private(*RakeFileUtils.instance_methods(false))

##################################################################



The problem is our friend "send" and the fact that it could care less about the call to private. This cost me the better part of a night, and I guess I've never really forgiven rake for it...

Enter thor. If you haven't heard of this, take a look here and see what you think. The code is practically 100% covered by specs, and is easy to dive into...and something just feels better about defining plain old ruby methods instead of tasks.

So, in the interest of learning thor better, I took the merb sake tasks and converted them to their thor equivalents. The end result is found at http://github.com/jackdempsey/merb-thor/tree/master and once you have thor installed you should install the file with
1
$ thor install http://github.com/jackdempsey/merb-thor/tree/master%2Fmerb.thor?raw=true --as=merb


The --as option lets you name things nicely so in the future you can do

$ thor update merb



to get any new stuff thats added in.

I'm happy to take comments, updates, patches, etc. Enjoy!

Update
There appears to be a small bug with namespacing of commands. For the time being, all of the commands should work if you run them inside a directory where merb.thor exists. I hope to have this fixed over the next few days, but til then, this should be a quick solution.

Update Part Deux
I've created a fork of thor in a branch you can find at: http://github.com/jackdempsey/thor/tree/constants

If you install from here thor will load all .thor files it finds in your root whether they're relevant or not. This will get around the constants issue preventing things like 'thor merb:gems:refresh' from working.

Update Part Finale
So merb-thor has grown up and become part of the merb stack. I've updated the repo to point to the new goodness, but if you're reading this you can go there directly:

http://merbunity.com/tutorials/18

Much thanks to those who've shown interest in it, sent me patches, etc, I appreciate it. I'll be working on new and exciting stuff with Thor in the future, and if you have thoughts/ideas on things like http://github.com/jackdempsey/self-deprecated let me know!

cdargs

I'm a fan of aliases and basically anything else that can help me type less when doing stuff on the command line. My home directory often has a number of symbolic links to various projects and libraries, and 'til now I've gotten by with this amalgamation of tools and techniques just fine.

Still, I'm always looking out for new tips and tricks, and one of the best and easiest ways to pick up some invaluable knowledge from others is to see how they use their shell. I forget sometimes that some of the shortcuts I know and love aren't used by others; I've had people say "wait, what was that?" when I do


$ mkdir something_here
$ cd !$


or even a simple !!, !mer, ctrl+r rake, etc. Still, you can only know so much, and recently I was that person saying "waaaiiit a sec..." while watching wycats fly around various directories with this little command 'cv'.

Enter cdargs



Take a look at its homepage here http://www.skamphausen.de/cgi-bin/ska/CDargs and come back when done scanning.

If you follow the instructions you'll have a nice and small 'cv' command at your disposal. Now, maybe it was related to the fact that I was doing this at 3am, but I definitely didn't get it at first, and its actually quite simple.

In browse mode you can easily traverse your file system and type 'a' along the way to add directories to your list. This means that you can just type 'cv' from anywhere, easily select something from your list, and change to it quickly. Nice, but still a little verbose for my liking. However, you can also use a name from your list and cv to it directly, which is just damn cool.

So, an example. My current list looks something like this:

0 [merb ] /Users/jackdempsey/git/merb
1 [thor ] /Users/jackdempsey/git/thor
2 [dm ] /Users/jackdempsey/git/dm
3 [git ] /Users/jackdempsey/git
4 [recollectr] !/Users/jackdempsey/recollectr
6 [testbed ] /Users/jackdempsey/testbed


Which means from any location i can type 'cv git' or 'cv merb' etc and jump to the directory directly. So much nicer than the 'cd ~/symbolic_link_here' that I've gotten used to.

So try it out. I think its one of those things that as soon as you build up the muscle memory for it, you'll never want to go back. Much thanks to Yehuda for showing me the light on this one.

Saturday, July 5, 2008

merb-auth, webrat, and merb_stories

Recently I've been working on getting some stories written for www.recollectr.com and given that I just added in MerbAuth support, I thought now would be a good time to dive back into the previously paused process.

If you're interested in using webrat for merb_stories, you'll want to get the fork that supports merb from here:

http://github.com/gwynm/webrat/tree/master


I'd previously had some stories working with webrat and edge merb, but it looked like the upgrade to using MerbAuth in slice form broke some things. After some investigation I realized that the namespacing that's used in merb-slices was slightly different than what the merb supported fork of webrat was happy with...gwynm's fork adds a cookies= method to Application as follows:


class Application < Merb::Controller
def cookies=(newcookies)
@_cookies = newcookies
end
end


This is fine except with MerbAuth, we have MerbAuth::Application, and as such things were busted a bit. So, doing the same for MerbAuth::Application fixed things right up:


module MerbAuth
class Application < Merb::Controller
def cookies=(newcookies)
@_cookies = newcookies
end
end
end


Thing is, this doesn't exactly belong in MerbAuth as its specific to webrat....and it doesn't exactly belong in webrat as its going to be an issue to any merb-slice. So for the time being I have this in my app code, and things work nicely. I'll probably write a little more and maybe give some examples for general webrat and merb_stories usage, but til then hope this helps anyone else trying all three of these great components.