portable perspectives

a blog about programming, life, universe and everything

Gen Refactored !

The fun of refactoring gen finally reached a local maximum with which I mean that I’m satisfied with the code even if not perfect and that it will need a new coding sprint to get past that.

The new code (patch) is very easy to understand: you find a class based on the first argument via Gen.load_generator & Gen.generator_class, then you instantiate it with new call run on it.

The error handling is done via a simple error method inherited in the various Gen subclasses, which allows consistent erro reporting.

I even put in my old idea for better error reporting when using generators, in the old days you’d get Cannot load the specified generator! in this cases:

  • wrong generator name
  • Syntax errors in the gen.rb file
  • failing require@s in the @gen.rb file

and every other LoadError, now a specific error message is supplied.

The only thing I’m not 100% happy with is the dependency of the code on a small naming convention, the classes have to be called SomethingGen where Gen is not optional. This allows to find them dynamically via a simple const_get and is far better imo, than relying on globals, because it allows simpler testing and reuse, but may not be really perfect.

I’m thinking of using ObjectSpace to find each subclass of Gen (facets already has a Class#subclasses method IIRC) but I have not thought much about it, because it seem seamingless complicated. Suggestions are welcome.

Funny Horror Story

Anyway, moving functionalities into lib/gen and out of the generators and the executable script allowed a nice removing of duplicated code, and now we have the nice feature of knowing for sure what are the available generators (in the old script they were hardcoded), but in the process I did encounter nasty bug that took me half an hour to fix.

Basically, the gen script used to load lib/gen.rb and had some code in its body, withouth any method def. The generators, OTOH had a lot of had duplicated require that could be pushed into the superclass.

So the first step of the refactoring was to define methods containing the script code. Check, everything working. Then move them to the Gen class. Check. Then move duplicated require from the generators. Fail.

Sadly,I did not run the tests after each step and the Curse Of The Dead Tester hit me in the face, all the generators were suddenly unable to access files that I supplied them (i.e. gen form model.rb was raising a LoadError) and I could not understand why.

I blamed win32 and Unix and ruby for a while, I did rituals to calm the bad spirits that ate my $LOAD_PATH and wondered where the hell the files generated from gen app were disappearing.

The I discovered that when using Nitro as a library you have to explicitly tell it to not change the working directory by setting the $NITRO_NO_ENVIRONMENT variable before you load the file and not before you use it.

So I could send the bad spirit to rest, and now everything is peaceful again.

AddThis Social Bookmark Button

Sorry, comments are closed for this article.