Tuesday, September 9, 2008

Thor finds Mjolnir

If you've been following the development of Thor, you'll know there's been a bug that affected commands with several layers of namespacing. Tonight I'm happy to announce this bug was finally crushed by Yehuda Katz.

Just to briefly describe the pain this has brought me, a few facts:

* Class.constants only gets the top level constants. If you have Foo::Bar::Baz, don't expect to see Baz in Foo.constants

* Struct is mean:



(You can imagine how the first point led me to the second, right? Yeah...fun.

* Remember, when you ask for constants on a class, it:

Returns an array of the names of the constants accessible in mod. This includes the names of constants in any included modules (example at start of section).

It _also_ returns constants defined in any class you inherit from.

So, lets just say this all added up to a nice bit of hacking for me, and truthfully it was a good experience. It started to get old after a while, but I think this is something people don't talk about enough. Its the same with lifting weights: you purposely destroy and rip apart your muscles so they can build up bigger and stronger next time.

If you don't at least semi-often tackle hard problems, frustrating situations, and just fight through those moments when you think "screw it, PHP isn't that bad", you'll never get the kind of experience that really makes you grow as a Rubyist, and really, a programmer. You'll often find at the end not only did you learn some new tricks, but sometimes the solution is right there before your eyes....or Yehuda's eyes...but still, you'll get a lot out of it.

So anyway, I'm happy to point people to http://github.com/wycats/thor/tree/master and say 'Enjoy!' The soon to come Merb bundling tasks will be taking advantage of Thor's power, so if you haven't done anything with Thor yet, now's a great time to jump on board.

Saturday, September 6, 2008

router redirects

Merb's router supports a nice redirect feature:

r.match('/old-invalid-url').redirect("/hawt-new-goodness")

One item of note--this redirect defaults to a 301 status, so if you're playing around with this and want 302's instead, pass a false as a second parameter to redirect:

r.match('/').redirect("/login", false)

If you happen to confuse this and set a permanent 301, just add in the false, hit the page again, let the 302 be processed, and you should be good after taking out the redirect again.

Thursday, September 4, 2008

A script to help with Rails -> Merb conversions

People have asked often in #merb for something to help with converting Rails apps to Merb apps. There's no magic Wizard to do this for you, but hopefully this will help:

http://github.com/jackdempsey/find_rails/tree/master

Its a standalone Thor script that looks in your current directory (or a provided path to an app), finds the app/ directory, and scans through the code for a variety of things that people get tripped up on in Merb. I imagine this list will increase over time, but it will help with some of the obvious ones to start.

This is very much a work in progress. Running it standalone like this, especially given the way I've organized it, loses some of the flexibility Thor provides. It also makes it easier on the user to invoke. I imagine if this is used by people and grows to be anything bigger than a simple helper script, I'll refactor the way the task is started, so I'd recommend cloning this and playing with the source a bit as well.

Anyway, if you haven't played with Thor yet, you should give it a go. Its fast, flexible, and a great way to avoid the "fun" of parsing options yourself.

Currently there's a bit of an issue with installing tasks that have multilevel namespaces. For the time being, I've committed a fix to the constants branch of my fork at http://github.com/jackdempsey/thor/tree/constants

I foresee this fix getting cleaned up and merged into the main Thor repo soon. In the meantime, if you're looking at using Thor with tasks like foo:bar:baz, this fork will fix things up for you.

Update

I fixed things up a bit so that we can search on regex's as well. This came out of a discussion in #merb re: the tendency to "redirect && return" in Rails, which is the reverse of what you'll want to do in Merb.

http://github.com/jackdempsey/find_rails/commit/ea80df077c8cee0959ce1e7b4b17321924f19dc7